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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:49:29+00:00 2026-05-28T05:49:29+00:00

All, I am missing something fundamental about the underlying model for Django’s ForeingKeys vs

  • 0

All,

I am missing something fundamental about the underlying model for Django’s ForeingKeys vs ManyToManyFields.

Suppose I am building an application about cars. I might have the following classes:

 class Car(models.Model):
   carName = models.CharField()

 class Manufacturer(models.Model):
   manufacturerName = models.CharField()

 class Wheel(models.Model):
   radius = models.IntegerField()

So far so good. Now there are some relations between these classes. A car has a manufacturer and has (four) tire(s). Conceptually, there is a difference though. The manufacturer is related via “aggregation”; a manufacturer can be associated to multiple cars; deleting a Car instance should not cause that car’s manufacturer to be deleted as well. The wheels are related via “composition”; every four wheels associated with a car are associated with that and only that car; delete the car and the wheels should be deleted as well.

So, intuitively, that means that I ought to do the following:

 class Car(models.Model):
   carName = models.CharField()
   manufacturer = models.ManyToManyField("Manufacturer")
   wheels = models.ForeignKey("Wheel")

Ultimately, I want to use inlineformset_factories so that users can fill in details about a car, its manufacturer and wheels all at the same time. Something like this:

 class CarForm(ModelForm):
   class Meta:
     model = Car

 class ManufacturerForm(ModelForm):
   class Meta:
     model = Manufacturer

 class WheelForm(ModelForm):
   class Meta:
     model = Wheel

 Manufacturer_formset = inlineformset_factory(Car,Manufacturer,formset=ManufacturerForm)
 Wheel_formset = inlineformset_factory(Car,Wheel,formset=WheelForm)

But most of the documentation that I find suggests that the ForiegnKey should go from Wheel to Car. This seems backwards to me, since the Wheel_formset would then present the user with all of the fields for a Car (“carName”) and not a Wheel (“radius”).

Just the act of typing this question is making me confused. Can anybody shed some light on how I can build a form that has all of a car fields, and then all of a manufacturer fields, and then all of a wheel fields.

Thanks

  • 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-28T05:49:29+00:00Added an answer on May 28, 2026 at 5:49 am

    If each car has one manufacturer, then you should use a foreign key from Car to Manufacturer. This will allow multiple cars to have the same manufacturer, and manufacturers will not be deleted when cars are deleted. A many to many field suggests that one car can have multiple manufacturers.

    Wheel should have a foreign key to Car. This will allow multiple wheels to have the same car, and the default Django behaviour when a car is deleted will be to delete the wheels.

    So your models should look something like this:

    class Manufacturer(models.Model):
        name = models.CharField()
    
    class Car(models.Model):
        name = models.CharField()
        manufacturer = models.ForeignKey("Manufacturer")
    
    class Wheel(models.Model):
        radius = models.IntegerField()
        car = models.ForeignKey("Car")
    

    For your view, I would first try to write views for the forms and formsets individually, and make sure you understand the relationships between your models before you bring them all together in one view.

    This Stack Overflow question explains how to use a form and inline formset together at the same time (equivalent to the Car and Wheel models in your case). For the manufacturer, you probably want to exclude the manufacturer field from your CarForm, then set it in your view before you save.

    ...
    manufacturer = ManufacturerForm.save()
    car = CarForm.save(commit=False)
    car.manufacturer = manufacturer
    car.save()
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think i'm missing something fundamental about how Hibernate works, specifically with lazy loading.
Multiple posts but I'm still stuck...I'm missing something fundamental here. I have a form
OK, I am missing something fundamental here I am sure! But for the life
Am I missing something fundamental to the principals of MVC or am I going
I'm new to NHibernate so I'm sure that I'm just missing something fundamental. I
Is there any command available for generating all missing spec files for existing models
git-cherry lets me find all the commits missing from upstream. How do I find
All I know about the constraint is it's name ( SYS_C003415 ), but I
All throughout an application wherever error messages (or other user messages) are used I
I'm getting all learned up about binding in WPF. I'm having a lot of

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.