与Django、Python的多对多关系
目前,我正在开发一个应用程序来管理用Python Django编写的汽车和事件的驱动程序 我有两种型号:与Django、Python的多对多关系,django,python-2.7,relation,Django,Python 2.7,Relation,目前,我正在开发一个应用程序来管理用Python Django编写的汽车和事件的驱动程序 我有两种型号: class Vehicle(models.Model): name = models.CharField(max_length=100) max_capacity = models.IntegerField(default=2) available = models.BooleanField() class Event(mod
class Vehicle(models.Model):
name = models.CharField(max_length=100)
max_capacity = models.IntegerField(default=2)
available = models.BooleanField()
class Event(models.Model):
start_timestamp = models.DateTimeField()
end_timestamp = models.DateTimeField()
description = models.TextField()
location = LatLonField()
我节省了10辆不同的车辆。有18个不同的项目 现在,我想将多辆车添加到一个事件中,而不为每个事件生成新的车辆,并指定将在此事件中使用此车的人数,如:
Event 0815:
- FirstCar, loaded with 5/13 People
- SecondCar, loaded with 2/2 People
(车型中给出了汽车的最大容量)
djangos数据库概念的结构是如何实现的?您可以通过ManyToMany
字段的参数创建并使用:
class Event(models.Model):
...
vehicles = models.ManyToManyField(Vehicle, through='EventVehicle')
class EventVehicle(models.Model):
event = models.ForeignKey(Event)
vehicle = models.ForeignKey(Vehicle)
loaded = models.IntegerField(default=2)
稍后我会尝试一下,但这似乎对我有帮助!请注意,对于中间模型,您必须使用管理内联线来编辑事件车辆列表,而不是简单的M2M字段.Perfect。这就是我需要的!谢谢