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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:31:12+00:00 2026-05-23T16:31:12+00:00

I have two models class Employer(models.Model): name = models.CharField(max_length=300, blank=False) id = models.IntegerField() status

  • 0

I have two models

class Employer(models.Model):
     name = models.CharField(max_length=300, blank=False)
     id = models.IntegerField()
     status = models.IntegerField()
     eminence = models.IntegerField(null=False,default=4)

class JobTitle(models.Model)
     name = models.CharField(max_length=300, blank=False)
     employer = models.ForeignKey(Employer,unique=False,null=True)
     activatedate = models.DateTimeField(default=datetime.datetime.now)

I need all employers in the order of whose jobtitle activated last.

Employer.objects.filter(status=1).order_by('eminence','-jobtitle__activatedate')

This query gives me what I want but it returns repeated employers if employer has more than one jobtitle.

I would use distinct() but in Django documents I found that

*Any fields used in an order_by() call are included in the SQL SELECT columns. This can sometimes lead to unexpected results when used in conjunction with distinct(). If you order by fields from a related model, those fields will be added to the selected columns and they may make otherwise duplicate rows appear to be distinct. Since the extra columns don’t appear in the returned results (they are only there to support ordering), it sometimes looks like non-distinct results are being returned.*

Although they explained my problem no solution is specified.

Could give me a suggestion how can I group by my employer list without corrupting the API stability?

  • 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-23T16:31:12+00:00Added an answer on May 23, 2026 at 4:31 pm

    You can place on the Employer class its latest JobTitle activation date, and then order by this field without using relations.[1] The tradeoff here is a bit of data duplication, and the necessity to update employer’s latest job title activation date manually when corresponding JobTitle instance changes.

    Also, check this post for another solution which uses annotate().

    Related questions:

    [1] Queryset API distinct() does not work?

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

Sidebar

Related Questions

I have two tables, one Company and one Employee: class Company(models.Model): name = models.CharField(max_length=60)
Short question. I have two models: class Author(models.Model): name = models.CharField(max_length=250) class Book(models.Model): title
My models: class ItemSet(models.Model): name = models.CharField(max_length=30) item = models.ManyToManyField(Item) order = models.IntegerField(default=0) class
I have two models: class Production(models.Model): gallery = models.ManyToManyField(Gallery) class Gallery(models.Model): name = models.CharField()
I have two models like this: class AA(models.Model): name = models.CharField() state = models.IngerField()
I have two models for store and city: class City(models.Model): name = models.CharField() slug
I have these two models : class Module(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(unique=True,
I have two models related by a foreign key: # models.py class TestSource(models.Model): name
Say I have these two models: public class City extends Model { @ManyToOne private
I have two models lets say: class superfields(Model): fieldA = models.FloatField() fieldB = models.FloatField()

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.