let’s assume, I have a model called Chicken:
class Chicken(models.Model):
name = models.CharField(max_length=30)
and I also have a model called Egg that has a ForeignKey to the Chicken-model:
class Egg(models.Model):
chicken = models.ForeignKey(Chicken)
layed_on = models.DateField(auto_add_now=True)
Now I want to create a view that contains a list of the all the chickens and the date when they layed their last egg.
I know how to get all eggs layed by a chicken, using something like this:
c = Chicken.objects.get(name='Henry')
e = c.egg_set.all()
But that doesn’t really help, because I have a lot of chickens and I want to only get the date of their latest egg. I also don’t know how to get that egg information into my view for easy access. In my view I would like to do something like this:
{% for chicken in chickens %}
<p>{{ chicken.name }} layed is last egg on {{ chicken.last_egg }}</p>
{% endfor %}
This is probably a very basic question, but I’m just a beginner and I would greatly appreciate any help you can give. The Django documentation does not really help me in this case.
What you’re looking for is django’s annotation feature. Basically, you want to annotate each row in chicken with the
Maxof egg’s layed_on field. Without a prompt right in front of me, I can’t guarentee the exact statement below, but it should give you a starting point.Edit your chicken model like this: