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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:31:51+00:00 2026-05-16T06:31:51+00:00

I have the field ‘submission’ which has a user and a problem. How can

  • 0

I have the field ‘submission’ which has a user and a problem. How can I get an SQL search result which will give a list of only one result per user-problem pair?

Models are like this:

class Problem(models.Model):
    title = models.CharField('Title', max_length = 100)
    question = models.TextField('Question')

class Submission(models.Model):
    user = models.ForeignKey(User)
    problem = models.ForeignKey(Problem)
    solution = models.CharKey()
    time = models.DateTimeField('Time', auto_now_add=True)
  • 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-16T06:31:51+00:00Added an answer on May 16, 2026 at 6:31 am

    Update 2:

    (After reading OP’s comments) I suggest adding a new model to track the latest submission. Call it LatestSubmission.

    class LatestSubmission(models.Model):
        user = models.ForeignKey(User)     
        problem = models.ForeignKey(Problem)
        submission = models.ForeignKey(Submission)
    

    You can then either

    1. override Submission.save() to create/update the entry in LatestSubmission every time an user posts a new solution for a Problem
    2. attach a function that does the same to a suitable signal.

    such that LatestSubmission will contain one row per problem-user-submission combination pointing to the latest submission for the problem by each user. Once you have this in place you can fire a single query:

    LatestSubmission.objects.all().order_by('problem')
    

    Update:

    Since the OP has posted sample code, the solution can now be changed as follows:

    for user in User.objects.all(): # Get all users
        user.submission_set.latest('time') # Pick the latest submission based on time.
    

    Original Answer

    In the absence of any date/time based criteria for deciding which is “older” or “newer”, you can use the primary key (id) of Submission to “neglect the old ones”.

    for user in User.objects.all(): # Get all users
        user.submission_set.latest('id') # Pick the latest submission by each user.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my database (SQL 2005) I have a field which holds a comment but
I have a field (say, foo) in a table in a SQL Server database
I have a field in my form labeled Name that will contain both the
i have field in databaase. which contains special characters. i want to escape these
Table has 1 500 000 records, 1 250 000 of them have field =
I have one field that I need to sum lets say named items However
I have a field on a table in MS Access, tblMyTable.SomeID, and I want
I have a field in a database that is nearly unique: 98% of the
I have a field in my report that needs to have a length of
I have a field object and I create a list of fields: class Field

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.