我在django中有一个issu,当我运行djangoshell并试图在数据库中添加一些值时,出现了一个错误。我用的是Python管理.py迁移和python管理.py迁移之前。你知道吗
>from store.models import Marque, Model
charger = Model(name="Charger")
charge.save()
---然后我有这个错误,如果我创建“Marque”对象,它工作正常
Null value in column “marque_id” violates not-null constraint
from django.db import models
# Create your models here.
class Contact(models.Model):
email = models.EmailField(max_length=100)
name = models.CharField(max_length=200)
class Marque(models.Model):
name = models.CharField(max_length=200, unique=True)
class Model(models.Model): #Multiples Model to One Marque
reference = models.IntegerField(null=True)
created_at = models.DateTimeField(auto_now_add=True)
available = models.BooleanField(default=True)
name = models.CharField(max_length=200)
picture = models.URLField()
marque = models.ForeignKey(Marque, on_delete=models.CASCADE)
class Booking(models.Model): #mutliples Booking to one Contact
created_at = models.DateTimeField(auto_now_add=True)
contacted = models.BooleanField(default=False)
marque = models.OneToOneField(Marque, on_delete=models.CASCADE)
model = models.OneToOneField(Model, on_delete=models.CASCADE)
contact = models.ForeignKey(Contact, on_delete=models.CASCADE)
Requirement.txt: Django==2.1.7 django-debug-toolbar==1.11 psycopg2==2.7.7 psycopg2-binary==2.7.7 pytz==2018.9 sqlparse==0.2.4
您的
Model
模型包含一个名为marque
的ForeignKey
:这是一个不可为空的
ForeignKey
(它不包含null=True
作为参数),但是您没有提供对Marque
对象的引用,因此出现了错误。你知道吗为了解决这个问题,您要么提供一个
Marque
对象,例如:或者可以使
ForeinKey
为null,但这当然会改变建模:相关问题 更多 >
编程相关推荐