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

  • Home
  • SEARCH
  • 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 3632038
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:28:29+00:00 2026-05-19T00:28:29+00:00

I have a list of Person s each which have multiple fields that I

  • 0

I have a list of Persons each which have multiple fields that I usually filter what’s upon, using the object_list generic view. Each person can have multiple Comments attached to them, each with a datetime and a text string. What I ultimately want to do is have the option to filter comments based on dates.

class Person(models.Model):
    name = models.CharField("Name", max_length=30)
    ## has ~30 other fields, usually filtered on as well

class Comment(models.Model):
    date = models.DateTimeField()
    person = models.ForeignKey(Person)
    comment = models.TextField("Comment Text", max_length=1023)

What I want to do is get a queryset like

Person.objects.filter(comment__date__gt=date(2011,1,1)).order_by('comment__date')

send that queryset to object_list and be able to only see the comments ordered by date with only so many objects on a page.

E.g., if “Person A” has comments 12/12/2010, 1/2/2011, 1/5/2011, “Person B” has no comments, and person C has a comment on 1/3/2010, I would see:

"Person A",   1/2  - comment
"Person C",   1/3  - comment
"Person A",   1/5  - comment

I would strongly prefer not to have to switch to filtering based on Comments.objects.filter(), as that would make me have to largely repeat large sections of code in the both the view and template.

Right now if I tried executing the following command, I will get a queryset returning (PersonA, PersonC, PersonA), but if I try rendering that in a template each persons comment_set will contain all their comments even if they aren’t in the date range.

Ideally there would be some sort of functionality where I could expand out a Person queryset’s comment_set into a larger queryset that can be sorted and ordered based on the comment and put into a object_list generic view. This normally is fairly simple to do in SQL with a JOIN, but I don’t want to abandon the ORM, which I use everywhere else.


OK, my solution was basically just Comments.objects.filter(); split the template into a separate file that is included in the appropriate fashion, apply extra context boolean (if comment_date_filter, apply an appropriate prefix string before all person references in the view (that’s either ”, or ‘person__’):

{% if comment_date_filter %}
   {% for obj in object_list %}
      {% with obj.person as p %}
        {% include "object_list_row.html" %}
      {% endwith %}
   {% endfor %}
{% else %}
   {% for obj in object_list %}
      {% with obj as p %}
        {% include "object_list_row.html" %}
      {% endwith %}
   {% endfor %}
{% endif %}
  • 1 1 Answer
  • 1 View
  • 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-19T00:28:29+00:00Added an answer on May 19, 2026 at 12:28 am

    The other option you have is to pull all objects out as (Person, Date, Comment) tuples and sort them in Python.

    However, I don’t know what your reason for not wanting to use Comment.objects.filter(). This is likely going to be fairly fast, especially if your date field is indexed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of person objects which I want to send in response
I have a list of Persons inside a Company Class. public class Company{ IList<Person>
I've been trying to use a List<> of a Model.Person that I have inside
I have a ListBox that is bound to a list of Persons. I want
Let's say I have a collection of Person objects, each of which looks like
A brief description... I have 4 tables. contacts (a list of each person, unique
Lets say I have a flat list of objects, each of which has a
Suppose I have a list of Person objects, which have an age and room_number
I'm implementing a system where I have a list of names and each person
I have a parent page which has a drop down list in it. using

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.