对 django 外键、manytomanyfield、inlineformset_factories 感到困惑
总之,
我缺少一些有关 Django 的 ForeingKeys 与 ManyToManyFields 底层模型的基本知识。
假设我正在构建一个关于汽车的应用程序。我可能有以下课程:
class Car(models.Model):
carName = models.CharField()
class Manufacturer(models.Model):
manufacturerName = models.CharField()
class Wheel(models.Model):
radius = models.IntegerField()
到目前为止一切顺利。现在这些类之间存在一些关系。一辆汽车有一个制造商并有(四个)轮胎。但从概念上讲,还是有区别的。制造商通过“聚合”关联;一个制造商可以与多辆汽车相关联;删除 Car 实例不应导致该汽车的制造商也被删除。轮子通过“组合”关联起来;与一辆车关联的每四个轮子都与该车且仅与该车关联;删除汽车,车轮也应该删除。
因此,直观上,这意味着我应该执行以下操作:
class Car(models.Model):
carName = models.CharField()
manufacturer = models.ManyToManyField("Manufacturer")
wheels = models.ForeignKey("Wheel")
最终,我想使用 inlineformset_factories,以便用户可以同时填写有关汽车、其制造商和车轮的详细信息。像这样的事情:
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)
但是我找到的大多数文档都表明 ForiegnKey 应该从 Wheel 到 Car。这对我来说似乎是倒退的,因为 Wheel_formset 将向用户呈现汽车(“carName”)而不是车轮(“半径”)的所有字段。
只是输入这个问题的行为就让我感到困惑。任何人都可以阐明我如何构建一个包含所有汽车字段、然后所有制造商字段、然后所有车轮字段的表单。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果每辆车都有一个制造商,那么您应该使用从
Car
到Manufacturer
的外键。这将允许多辆汽车拥有相同的制造商,并且删除汽车时不会删除制造商。多对多字段表明一辆车可以有多个制造商。Wheel
应该有一个Car
的外键。这将允许多个轮子拥有相同的汽车,并且删除汽车时 Django 的默认行为将是删除轮子。因此,您的模型应该如下所示:
对于您来说,我首先尝试分别为表单和表单集编写视图,并确保您了解模型之间的关系,然后再将它们全部放在一个视图中。
这个Stack Overflow问题解释了如何使用表单和内联表单集同时组合在一起(相当于您案例中的
Car
和Wheel
模型)。对于制造商,您可能需要从
CarForm
中排除manufacturer
字段,然后在保存之前将其设置在视图中。If each car has one manufacturer, then you should use a foreign key from
Car
toManufacturer
. 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 toCar
. 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:
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
andWheel
models in your case). For the manufacturer, you probably want toexclude
themanufacturer
field from yourCarForm
, then set it in your view before you save.