Django 非空约束失败:cars\u car.owner\u id
我能够将表单呈现到html上,输入数据并提交它,但是我遇到了NOTNULL约束失败。正如我在我的观点中所指出的,所有者不是被分配给其各自的所有者吗?我不知道这里出了什么问题,请帮忙Django 非空约束失败:cars\u car.owner\u id,django,django-forms,Django,Django Forms,我能够将表单呈现到html上,输入数据并提交它,但是我遇到了NOTNULL约束失败。正如我在我的观点中所指出的,所有者不是被分配给其各自的所有者吗?我不知道这里出了什么问题,请帮忙 型号 class Car(models.Model): owner = models.ForeignKey('auth.User', on_delete=models.CASCADE) name = models.CharField(max_length=100) model = model
型号
class Car(models.Model):
owner = models.ForeignKey('auth.User', on_delete=models.CASCADE)
name = models.CharField(max_length=100)
model = models.CharField(max_length=100)
description = models.TextField()
image = models.ImageField(upload_to=upload_image_path, null=True, blank=True)
created = models.DateField(auto_now_add=True)
updated = models.DateField(auto_now_add=False)
mileage = models.IntegerField()
open_market_value = models.DecimalField(max_digits=12, decimal_places=2)
depreciation = models.DecimalField(max_digits=10, decimal_places=2)
down_payment = models.DecimalField(max_digits=10, decimal_places=2)
road_tax = models.DecimalField(max_digits=8, decimal_places=2)
installment = models.DecimalField(max_digits=8, decimal_places=2)
objects = models.Manager()
def __str__(self):
return self.name
class CarCreate(CreateView):
model = Car
fields = [
'name', 'model',
'description', 'image',
'updated', 'mileage',
'open_market_value', 'depreciation',
'down_payment', 'road_tax',
'installment']
template_name = 'cars/create_car.html'
def form_valid(self, form):
form.instance.created_by = self.request.user
return super().form_valid(form)
视图
class Car(models.Model):
owner = models.ForeignKey('auth.User', on_delete=models.CASCADE)
name = models.CharField(max_length=100)
model = models.CharField(max_length=100)
description = models.TextField()
image = models.ImageField(upload_to=upload_image_path, null=True, blank=True)
created = models.DateField(auto_now_add=True)
updated = models.DateField(auto_now_add=False)
mileage = models.IntegerField()
open_market_value = models.DecimalField(max_digits=12, decimal_places=2)
depreciation = models.DecimalField(max_digits=10, decimal_places=2)
down_payment = models.DecimalField(max_digits=10, decimal_places=2)
road_tax = models.DecimalField(max_digits=8, decimal_places=2)
installment = models.DecimalField(max_digits=8, decimal_places=2)
objects = models.Manager()
def __str__(self):
return self.name
class CarCreate(CreateView):
model = Car
fields = [
'name', 'model',
'description', 'image',
'updated', 'mileage',
'open_market_value', 'depreciation',
'down_payment', 'road_tax',
'installment']
template_name = 'cars/create_car.html'
def form_valid(self, form):
form.instance.created_by = self.request.user
return super().form_valid(form)
HTML
{%extends'base.html%}
{%load crispy_forms_tags%}
{%block content%}
{%csrf_令牌%}
{{form | crispy}}
{%endblock%}
这很可能是因为车主
是车型中的必填字段,但您尚未将其包含在CreateView的字段中 您的模型具有来自“django.auth”的用户模型外键。当您试图保存'Car'模型的对象时,因为模型的'owner'字段中没有提到对象,它显示了错误。所以,你可能想明确地提到它
你可以这样做。假设您有“CarForm”,这是您的“Car”模型的模型形式
user = request.user
car_form = CarForm(request.POST)
if car_form.is_valid():
car = car_form.save(False)
car.owner = user
car.save()