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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:29:13+00:00 2026-05-20T19:29:13+00:00

OK I know the title sounds really confusing, but really it’s quite simple. Consider

  • 0

OK I know the title sounds really confusing, but really it’s quite simple. Consider this:

class A(models.Model):
    field = models.CharField(max_length=10)

class B(models.Model):
    field = models.CharField(max_length=10)
    a_elements = models.ManyToManyField(A)

class C(models.Model):
    field = models.CharField(max_length=10)
    b_element = models.ForeignKey(A)

so, now what I want is to extend all instances of A with some other field. for example for every instance of A that is available via B i need an integer associated to it.

is there an easy way of doing this?

EDIT:

I think one can see this as a definition of one-to-many relationship from C to instances of A in B, if that makes sense…

  • 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-20T19:29:14+00:00Added an answer on May 20, 2026 at 7:29 pm

    If you need to extend all instances of A with another field, add another field.

    For your example scenario of needing a field for every A through B, use a through model.

    http://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships

    class A(models.Model):
        field = models.CharField(max_length=10)
    
    class B(models.Model):
        field = models.CharField(max_length=10)
        a_elements = models.ManyToManyField(A, through='B_A')
    
    class C(models.Model):
        field = models.CharField(max_length=10)
        b_element = models.ForeignKey(A)
    
    
    class B_A(models.Model):
        """
        Custom through model for B.a_elements
        """
        a = models.ForeignKey(A)
        b = models.ForeignKey(B)
        integer = models.IntegerField()
    
    
    b = B.objects.latest('id')
    ab_elements = B_A.objects.filter(b=b).select_related()
    # these are your m2m intermediary model instances, 
    # which are essentially A instances with an extra field.
    
    for ab in ab_elemements:
        print ab.a # a element
        print ab.integer # integer associated with this a element.
    

    By your question title, I think you’re asking a question about C though (which you didn’t mention)

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

Sidebar

Related Questions

Hey all. I know this sounds simple, but I can't find a way to
I know the title sounds funny, but I found this snippet somewhere: my MyPackage
I know the title of the question sounds absolutely weird but I had no
I know the title makes this sound very easy, but I have a For
The title may sound confusing but it actually isn't, I just didn't know how
I know the title is somewhat confusing, my problem is this: I want to
I know that title sounds crazy but here is my situation. After a certain
I know the title sounds familiar as there are many similar questions, but I'm
Morning all, I know that this sounds like a simple referencing problem from the
Hi i know the Title might sound a little confusing but im reading in

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.