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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:26:35+00:00 2026-06-14T03:26:35+00:00

I have two models class Subject(models.Model): name = models.CharField(max_length=100,choices=COURSE_CHOICES) created = models.DateTimeField(‘created’, auto_now_add=True) modified

  • 0

I have two models

class Subject(models.Model):
    name = models.CharField(max_length=100,choices=COURSE_CHOICES)
    created = models.DateTimeField('created', auto_now_add=True)
    modified = models.DateTimeField('modified', auto_now=True)
    syllabus = models.FileField(upload_to='syllabus')
    def __unicode__(self):
        return self.name

and

class Pastquestion(models.Model):
    subject=models.ForeignKey(Subject)
    year =models.PositiveIntegerField()
    questions = models.FileField(upload_to='pastquestions')
    def __unicode__(self):
        return str(self.year)

Each Subject can have one or more past questions but a past question can have only one subject. I want to get a subject, and get its related past questions of a particular year. I was thinking of fetching a subject and getting its related past question.

Currently am implementing my code such that I rather get the past question whose subject and year correspond to any specified subject like

this_subject=Subject.objects.get(name=the_subject)
thepastQ=Pastquestion.objects.get(year=2000,subject=this_subject)

I was thinking there is a better way to do this. Or is this already a better way? Please Do tell ?

  • 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-14T03:26:36+00:00Added an answer on June 14, 2026 at 3:26 am

    I think what you want is the related_name property of the ForeignKey field. This creates a link back to the Subject object and provides a manager you can use to query the set.

    So to use this functionality, change the foreignkey line to:

    subject=models.ForeignKey(Subject, related_name='questions')
    

    Then with an instance of Subject we’ll call subj, you can:

    subj.questions.filter(year=2000)
    

    I don’t think this performs much differently to the technique you have used. Roughly speaking, SQL performance boils down a) whether there’s an index and b) how many queries you’re issuing. So you need to think about both. One way to find out what SQL your model usage is generating is to use SqlLogMiddleware – and alternatively play with the options in How to show the SQL Django is running It can be tempting when you get going to start issuing queries across relationships – e.g. q = Question.objects.get(year=2000, subject__name=SUBJ_MATHS) but unless you keep a close eye on these types of queries, you can and will kill your app’s performance, badly.

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

Sidebar

Related Questions

I have two models: class Contact(models.Model): name = models.CharField(max_length=255) class Campaign(models.Model): contact = models.ForeignKey(Contact,
I have two models: class Actor(models.Model): name = models.CharField(max_length=30, unique = True) event =
In Django, I have two models: class Product(models.Model): name = models.CharField(max_length = 50) categories
I have two models with slug fields: class Book(models.Model): name = models.CharField(max_length=200) slug =
I have two models class Status(models.Model) name = models.CharField(max_length=25) rank = models.PositiveSmallIntegerField() class MyModel(models.Model)
I have two models: class Product(models.Model): name = models.CharField(max_length=255) class ProductPhoto(models.Model): product = models.ForeignKey('Product',
I have two models class Employer(models.Model): name = models.CharField(max_length=300, blank=False) id = models.IntegerField() status
I have two models: class Studio(models.Model): name = models.CharField(Studio, max_length=30, unique=True) class Film(models.Model): studio
I have following two models class Questionnaire(models.model) name = models.CharField(max_length=128, null=True, blank=True) type =
I have two models: class Studio(models.Model): name = models.CharField(Studio, max_length=30, unique=True) class Film(models.Model): studio

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.