I have these models,
class Place(models.Model):
name = models.CharField(max_length=200)
user = models.ForeignKey(User, on_delete=models.CASCADE,
related_name='places', default=1)
subscribers = models.ManyToManyField(AppUser, through='PlaceSubscriber')
def __str__(self): # __unicode__ on Python
return self.name
class PlaceSubscriber(models.Model):
place = models.ForeignKey(Place, on_delete=models.CASCADE)
user = models.ForeignKey(AppUser, on_delete=models.CASCADE)
date_subscribed = models.DateTimeField(editable=False, default=timezone.now)
class Meta:
unique_together = ('place', 'user')
I want to access the date_subscribed fields on this for loop inside my template
{% for o in place.subscribers.all %}
<a href="#" class="list-group-item clearfix">
<span class="pull-left">
{{ forloop.counter }}.  
</span>
<span class="pull-left">
<strong>{{ o.full_name }}</strong>
<p>Email: <i>{{ o.email }}</i> | Date Subscribed: <i> {{
o.place__placesubscriber__date_subscribed }} </i> </p>
</span>
<span class="pull-right">
<span class="btn btn-xs btn-primary"
onclick="sendPushNotificationToUser('{{ o.ionic_id }}'); return
false;">Send Message</span>
<span class="btn btn-xs btn-danger" onclick="deletePlaceUser({{ place.id
}}, {{ o.id }}); return false; ">
Unsubscribe
</span>
</button>
</span>
</a>
{% endfor %}
I can access the date_subscribed field outside this foorloop like this:
{% for each in place.placesubscriber_set.all %}
{{ each.date_subscribed }}
{% endfor %}
But havenĀ“t figure out how to it access inside the other one.
UPDATE
This is my view
class PlaceDetailView(DetailView):
model = Place
template_name = 'place/place_detail.html'
And this is the url pattern
url(r'^place/(?P<pk>\d+)$', views.PlaceDetailView.as_view(), name='detail_place'),
I usually try to make a good query to deal with simple data structure in my template, avoiding complicated logic.
I would replace the view for a simple one that makes the query that you want (hopefully):
This generates a list of dictionaries with the query keys. You can display this data now by:
Well, I'm not sure if is that you want, but you can change the query values to get exactly what you want.
Hope it helps.