English 中文(简体)
混淆了django 外key、许多原子领域、线形-因素
原标题:confused about django foreignkey, manytomanyfield, inlineformset_factories

所有人,

我对Django s ForeingKeys诉许多ToMany Fields案的基本模式缺乏一些基本内容。

附录一 我也许有以下几类:

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

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

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

迄今情况良好。 现在这些阶层之间有一些关系。 一辆汽车有制造商,有(四)轮胎。 从概念上讲,情况有所不同。 制造商通过“行李”进行联系;制造商可以与多辆汽车连接;拆开车不应造成汽车制造商被删除。 轮椅通过“配对”进行;每四轮汽车与汽车有关,只有汽车;删除汽车和轮椅也应删除。

因此,间接来说,这意味着我应该做以下工作:

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

最后,我要使用线形——要素,使用户能够同时填写有关汽车、汽车制造商和轮船的细节。 与此类似:

 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应当从轮到车。 这对我来说似乎落后了,因为轮式“制”会向使用者介绍所有的田地,供他们使用汽车(“carName”)而不是轮式(“radius”)。

只是把这一问题打上了字,使我感到困惑。 任何人都能够说明我如何建立一个拥有所有汽车田的表格,然后是所有制造商田,然后是所有轮椅田。

增 编

最佳回答

如果每辆汽车有一个制造商,那么你就应当使用<代码>Car至Manufactur的外籍钥匙。 这将使多辆汽车有同样的制造商,在汽车被删除时不会删除制造商。 许多人认为,一辆汽车可以有多个制造商。

Car 。 这将使多轮轮轮车有相同的车,如果汽车被删除,Django的违约行为就是要删除轮椅。

因此,你的模型应当研究这样的情况:

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")

阁下认为,我首先要尝试就表格和表格分别发表看法,并确保你了解你的模式之间的关系,然后把所有这些模式汇集在一起。

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()
...
问题回答

暂无回答




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签