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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:40:42+00:00 2026-06-17T07:40:42+00:00

Model: class Subjects(models.Model): name = models.CharField(max_length=100) places = models.CharField(max_length=100) class Student(models.Model): name = models.CharField(max_length=40)

  • 0

Model:

class Subjects(models.Model):
    name = models.CharField(max_length=100)
    places = models.CharField(max_length=100)


class Student(models.Model):
    name = models.CharField(max_length=40)
    lastname = models.CharField(max_length=80)
    subjects = models.ManyToManyField(Subjects, blank=True)

Django creates appname_student_subjects when I use model above.

appname_student_subjects table looks for example, like this:

id   |    student_id   |  subjects_id
-----------------------------------------
1    |    1            |  10
2    |    4            |  11
3    |    4            |  19
4    |    5            |  10
...
~1000

How can I access subjects_id field and count how many times subjects_id exists in the table above (and then do something with it). For example: If subject with id 10 exists two times the template displays 2. I know that I should use "len" with result but i don’t know how to access subject_id field.
With foreign keys I’m doing it like this in a for loop:

results_all = Students.objects.filter(subject_id='10')
result = len(results_all)

and I pass result to the template and display it within a for loop but it’s not a foreign key so it’s not working.

  • 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-17T07:40:43+00:00Added an answer on June 17, 2026 at 7:40 am

    You can access the through table directly.

    num = (
        Student.subjects          # M2M Manager
          .through                # subjects_student through table
          .objects                # through table manager
          .filter(student_id=10)  # your query against through table
          .count()
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please see the following Django models: - class Student(models.Model): reference_num = models.CharField(max_length=50, unique=True) name
Model: class Subject(models.Model): name = models.CharField(max_length=100) slug = models.SlugField(unique=True) description = models.TextField(blank=True,null=True) Forms: class
I have a django model as below class MySubject(models.Model): name=models.CharField(unique=True,max_length=50) description=models.TextField(blank=True) slug=models.SlugField(editable=False) class Meta:
Here is my model: class Teacher(models.Model): name = models.CharField(max_length=50) subjects = models.ManyToManyField(Subject) class Subject(models.Model):
I have Django models as follows: class Subject(models.Model): name = models.CharField(max_length=35, unique=True) class Book(models.Model):
Model: class Subject(models.Model): name = models.CharField(max_length=50) places = models.IntegerField() class Temp(models.Model): subject_r = models.ForeignKey(Subject)
I have these models: class Person(models.Model): name=models.CharField(max_length=100) family=models.CharField(max_length=100) class MailContact(models.Model): person=models.ForeignKey(Person) email=models.CharField(max_length=100) #some fields
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
I have two models: class Studio(models.Model): name = models.CharField(Studio, max_length=30, unique=True) class Film(models.Model): studio
Model: class Program (models.Model): name = models.CharField(max_length=70) add = models.DateTimeField(auto_now=True) class Subject (models.Model): s_name

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.