Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 向数据库添加数据(中间模型)_Django_Django Orm - Fatal编程技术网

Django 向数据库添加数据(中间模型)

Django 向数据库添加数据(中间模型),django,django-orm,Django,Django Orm,如果我有中间车型,如何将汽车(汽车)添加到车库(车库)中?我不能理解这一点 class Car(models.Model): name = models.CharField(max_length=50) price = models.DecimalField() class GarageCar(models.Model): car = models.ForeignKey('Car') quantity = models.IntegerField() c

如果我有中间车型,如何将汽车(
汽车
)添加到车库(
车库
)中?我不能理解这一点

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')
观点


如果我有两个模型(汽车和车库,带野战车=模型。ManyToManyField(“汽车”),我会创建如下内容:

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

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

  • 中级车型
    GarageCar
    需要有一把外键,用于
    Car
    Garage
  • 定义多对多字段时,请使用
    参数指定中间表
  • 按如下方式更改您的型号:

     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')
    
    然后,您可以使用以下内容将汽车添加到车库:

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

    有关更多信息,请参阅上的文档。

    首先,您需要对您的型号进行几项更改:

  • 中级车型
    GarageCar
    需要有一把外键,用于
    Car
    Garage
  • 定义多对多字段时,请使用
    参数指定中间表
  • 按如下方式更改您的型号:

     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')
    
    然后,您可以使用以下内容将汽车添加到车库:

    GarageCar.objects.create(car=car,
                             garage=garage,
                             quantity=1,
                             )
    
    有关更多信息,请参阅上的文档