将数据添加到数据库(中间模型)

时间:2022-02-12 08:25:07

How can I add a car(Car) to garage (Garage) if I have an intermediate model? I can not understand this.

如果我有中间车型,如何将车(车)添加到车库(车库)?我不明白这。

class Car(models.Model):
    name = models.CharField(max_length=50)
    price = models.DecimalField()    

class GarageCar(models.Model):
    car = models.ForeignKey('Car')
    quantity = models.IntegerField()

class Garage(models.Model):
    name = models.CharField("Garage_Name", max_length=30)
    cars = models.ManyToManyField('GarageCar', blank=True, null=True)
    owner = models.ForeignKey(User, related_name='owner_garage', verbose_name='Owner Garage')

views

def add_car(request, car_id):

If I have two models (Car and Garage with field cars = models.ManyToManyField('Car') I create something like this:

如果我有两个型号(Car and Garage with field cars = models.ManyToManyField('Car')我创建这样的东西:

def add_car(request, car_id):
    if request.user.is_authenticated():
        user = request.user
        car = Car.objects.get(id = car_id)
        e = car.garage_set.create(name='example_name', owner=user)

    return render_to_response('add.html')

1 个解决方案

#1


1  

First, you need to make a couple of changes to your models:

首先,您需要对模型进行一些更改:

  1. The intermediate model GarageCar needs to have a foreign key to Car and Garage.
  2. 中间模型GarageCar需要拥有Car and Garage的外键。

  3. When you define the many to many field, use the through argument to specify the intermediate table.
  4. 定义多对多字段时,请使用through参数指定中间表。

Change your models as follows:

更改模型如下:

 class GarageCar(models.Model):
    car = models.ForeignKey('Car')
    garage = models.ForeignKey('garage')
    quantity = models.IntegerField()

class Garage(models.Model):
    name = models.CharField("Garage_Name", max_length=30)
    cars = models.ManyToManyField('Car', through='GarageCar')

Then, you can add a car to a garage with the following:

然后,您可以使用以下内容将汽车添加到车库:

GarageCar.objects.create(car=car,
                         garage=garage,
                         quantity=1,
                         )

See the docs on extra fields on many-to-many relationships for more information.

有关详细信息,请参阅多对多关系的额外字段文档。

#1


1  

First, you need to make a couple of changes to your models:

首先,您需要对模型进行一些更改:

  1. The intermediate model GarageCar needs to have a foreign key to Car and Garage.
  2. 中间模型GarageCar需要拥有Car and Garage的外键。

  3. When you define the many to many field, use the through argument to specify the intermediate table.
  4. 定义多对多字段时,请使用through参数指定中间表。

Change your models as follows:

更改模型如下:

 class GarageCar(models.Model):
    car = models.ForeignKey('Car')
    garage = models.ForeignKey('garage')
    quantity = models.IntegerField()

class Garage(models.Model):
    name = models.CharField("Garage_Name", max_length=30)
    cars = models.ManyToManyField('Car', through='GarageCar')

Then, you can add a car to a garage with the following:

然后,您可以使用以下内容将汽车添加到车库:

GarageCar.objects.create(car=car,
                         garage=garage,
                         quantity=1,
                         )

See the docs on extra fields on many-to-many relationships for more information.

有关详细信息,请参阅多对多关系的额外字段文档。