Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7676159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:08:07+00:00 2026-05-31T17:08:07+00:00

let’s assume, I have a model called Chicken: class Chicken(models.Model): name = models.CharField(max_length=30) and

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:08:09+00:00Added an answer on May 31, 2026 at 5:08 pm

    What you’re looking for is django’s annotation feature. Basically, you want to annotate each row in chicken with the Max of 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:

    class Egg(models.Model):
        chicken = models.ForeignKey(Chicken, related_name='eggs')
        layed_on = models.DateField(auto_add_now=True)
    
    from django.db.models import Max
    Chicken.objects.annotate(last_egg=Max('eggs__layed_on'))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have an abstract parent class called shape, and that there are
Let's say I have the following models class Photo(models.Model): tags = models.ManyToManyField(Tag) class Tag(models.Model):
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say you have a class library project that has any number of supplemental
Let's suppose I have a Discussion model, and that a discussion can be tagged.
Let me try to explain what I need. I have a server that is
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's assume that we are building a high traffic site that will be used

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.