I have two models like this:
class GuestStatus(models.Model):
guest_status = models.CharField(max_length=200)
arrangement = models.IntegerField(unique=True, help_text="Start from 1. Guest status will be arranged alphabetically.")
class Guest(models.Model):
user = models.ForeignKey(User)
full_name = models.CharField(max_length=250)
street_address = models.CharField(max_length=250, blank=True, null=True)
city = models.CharField(max_length=150, blank=True, null=True)
state = models.CharField(max_length=120, blank=True, null=True)
zip_code = models.CharField(max_length=15, blank=True, null=True)
status = models.ManyToManyField(GuestStatus, blank=True, null=True)
invitation_date = models.DateTimeField(blank=True, null=True)
I am trying to retrieve data in template:
#Views.py:
guests = Guest.objects.filter(user_id=request.user.id)
# Template:
{% for guest in guests %}
<tr>
<td width="5%"><input type="checkbox" value="{{ guest.id }}" name="guest_name" id="{{ forloop.counter }}" /></td>
<td><a href="/{{ guest.id }}/guest/">{{ guest.full_name }}</td>
<td>{{ guest.guests }}</td>
<td>{{ guest.children }}</td>
<td>{% for i in guest.gueststatus_set.all %}{{ i.status }}{% endfor %}</td>
</tr>
{% endfor %}
This is not giving any result for gueststatus. What's wrong?
For ManyToMany you don't get _set, change your query to guest.status.all(), also use {{i.guest_status}} as GuestStatus has guest_status not status.
<td>{% for i in guest.status.all %}{{ i.status }}{% endfor %}</td>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With