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 8928649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:29:31+00:00 2026-06-15T08:29:31+00:00

I’d like to create a filter-sort mixin for following values and models: class Course(models.Model):

  • 0

I’d like to create a filter-sort mixin for following values and models:

class Course(models.Model):
    title = models.CharField(max_length=70)
    description = models.TextField()
    max_students = models.IntegerField()
    min_students = models.IntegerField()
    is_live = models.BooleanField(default=False)
    is_deleted = models.BooleanField(default=False)
    teacher = models.ForeignKey(User)

class Session(models.Model):
    course = models.ForeignKey(Course)
    title = models.CharField(max_length=50)
    description = models.TextField(max_length=1000, default='')
    date_from = models.DateField()
    date_to = models.DateField()
    time_from = models.TimeField()
    time_to = models.TimeField()

class CourseSignup(models.Model):
    course = models.ForeignKey(Course)
    student = models.ForeignKey(User)
    enrollment_date = models.DateTimeField(auto_now=True)

class TeacherRating(models.Model):
    course = models.ForeignKey(Course)
    teacher = models.ForeignKey(User)
    rated_by = models.ForeignKey(User)
    rating = models.IntegerField(default=0)
    comment = models.CharField(max_length=300, default='')
  • A Course could be ‘Discrete mathematics 1’
  • Session are individual classes related to a Course (e.g. 1. Introduction, 2. Chapter I, 3 Final Exam etc.) combined with a date/time
  • CourseSignup is the “enrollment” of a student
  • TeacherRating keeps track of a student’s rating for a teacher (after course completion)

I’d like to implement following functions

  • Sort (asc, desc) by Date (earliest Session.date_from), Course.Name
  • Filter by: Date (earliest Session.date_from and last Session.date_to), Average TeacherRating (e.g. minimum value = 3), CourseSignups (e.g. minimum 5 users signed up)
    (these options are passed via a GET parameters, e.g. sort=date_ascending&f_min_date=10.10.12&…)

How would you create a function for that?

I’ve tried using

  • denormalization (just added a field to Course for the required filter/sort criterias and updated it whenever changes happened), but I’m not very satisfied with it (e.g. needs lots of update after each TeacherRating).
  • ForeignKey Queries (Course.objects.filter(session__date_from=xxx)), but I might run into performance issues later on..

Thanks for any tipp!

  • 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-06-15T08:29:32+00:00Added an answer on June 15, 2026 at 8:29 am

    In addition to using the Q object for advanced AND/OR queries, get familiar with reverse lookups.

    When Django creates reverse lookups for foreign key relationships. In your case you can get all Sessions belonging to a Course, one of two ways, each of which can be filtered.

    c = Course.objects.get(id=1)
    sessions = Session.objects.filter(course__id=c.id) # First way, forward lookup.
    sessions = c.session_set.all() # Second way using the reverse lookup session_set added to Course object.
    

    You’ll also want to familiarize with annotate() and aggregate(), these allow you you to calculate fields and order/filter on the results. For example, Count, Sum, Avg, Min, Max, etc.

    courses_with_at_least_five_students = Course.objects.annotate(
        num_students=Count('coursesignup_set__all')
    ).order_by(
        '-num_students'
    ).filter(
        num_students__gte=5
    )
    
    
    course_earliest_session_within_last_240_days_with_avg_teacher_rating_below_4 = Course.objects.annotate(
        min_session_date_from = Min('session_set__all')
    ).annotate(
        avg_teacher_rating = Avg('teacherrating_set__all')
    ).order_by(
        'min_session_date_from',
        '-avg_teacher_rating'
    ).filter(
        min_session_date_from__gte=datetime.now() - datetime.timedelta(days=240)
        avg_teacher_rating__lte=4
    )
    

    The Q is used to allow you to make logical AND and logical OR in the queries.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Let's say I'm outputting a post title and in our database, it's Hello Y’all
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to count the length of a string with PHP. The string

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.