Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.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 非空约束失败:cars\u car.owner\u id_Django_Django Forms - Fatal编程技术网

Django 非空约束失败:cars\u car.owner\u id

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

我能够将表单呈现到html上,输入数据并提交它,但是我遇到了NOTNULL约束失败。正如我在我的观点中所指出的,所有者不是被分配给其各自的所有者吗?我不知道这里出了什么问题,请帮忙


型号

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