简体   繁体   English

Django Help:使用CreateView解决NOT NULL约束失败错误

[英]Django Help: Resolving NOT NULL constraint failed error with CreateView

I am new to stackoverflow, python and Django, have a question on the '(NOT NULL constraint failed: sixerrapp_gig.user_id)' error I am getting when i click submit on the form. 我是stackoverflow,python和Django的新手,对我在表单上单击提交时遇到的'(NOT NULL约束失败:sixerrapp_gig.user_id)'错误有疑问。

Pretty sure it is because I have not assigned a user_id value from what I have found on StackOverflow thus far, but I am not sure how to do it using the django CreateView. 很确定这是因为我没有从StackOverflow上找到的user_id值分配到目前为止,但是我不知道如何使用django CreateView来完成它。

The project exercise is based on a clone of fiverr.com. 项目练习基于fiverr.com的克隆。

Please see code below: 请参阅以下代码:

in models.py: 在models.py中:

from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
from django.core.urlresolvers import reverse #for model forms

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    avatar = models.CharField(max_length=500)
    about = models.CharField(max_length=1000)

    def __str__(self):
        return self.user.username

class Gig(models.Model):
    CATEGORY_CHOICES = (
        ("GD", "Graphics & Design"),
        ("DM", "Digital & Marketing"),
        ("VA", "Video & Animation"),
        ("MA", "Music & Audio"),
        ("PT", "Programming & Tech")
    )
    title = models.CharField(max_length=500)
    category = models.CharField(max_length = 2, choices=CATEGORY_CHOICES)
    description = models.CharField(max_length=1000)
    price = models.IntegerField(default=6)
    photo = models.FileField(upload_to='gigs')
    status = models.BooleanField(default=True)
    user = models.ForeignKey(User)
    create_time = models.DateTimeField(default=timezone.now)

    def get_absolute_url(self):
        return reverse('my_gigs')

    def __str__(self):
        return self.title

views.py: views.py:

from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.views import generic
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from .models import Gig
from .forms import GigForm

class CreateGig(CreateView):
    model = Gig    
    fields = ['title','category','description','price','photo','status']

gig_form.html gig_form.html

{% extends 'base.html' %}
{% load staticfiles %}

{% block page %}

<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% include 'sixerrapp/form-template.html' %}
<button type="submit" class="btn btn-primary">Submit</button>
</form>

{% endblock %}

What i think i need is for the user field to get a value automatically when the form is submitted. 我认为我需要的是用户字段在提交表单时自动获取值。 Not sure what I am missing in my code to do that. 不知道我的代码中缺少什么来做到这一点。

Thanks in advance for the help! 在此先感谢您的帮助!

You need to handle that in form_valid() method of the view. 您需要在视图的form_valid()方法中处理它。

class CreateGig(CreateView):
    model = Gig    
    fields = ['title','category','description','price','photo','status']

    def form_valid(self, form):
        form.instance.user = self.request.user
        return super(CreateGig, self).form_valid(form)

More reference at https://docs.djangoproject.com/en/1.9/topics/class-based-views/generic-editing/#models-and-request-user 有关更多参考, 访问https://docs.djangoproject.com/en/1.9/topics/class-based-views/generic-editing/#models-and-request-user

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM