Django帮助:使用CreateView解决NOT NULL约束失败的错误 [英] Django Help: Resolving NOT NULL constraint failed error with CreateView
问题描述
我是stackoverflow,python和Django的新手,我对(没有NULL约束失败:sixerrapp_gig.user_id)"错误有疑问,我在表单上单击提交".
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上找到的值分配user_id值,但是我不确定如何使用django CreateView进行操作.
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.
项目练习基于Fiverr.com的副本.
The project exercise is based on a clone of fiverr.com.
请参见下面的代码:
在models.py中:
in 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:
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.
预先感谢您的帮助!
推荐答案
您需要在视图的 form_valid()
方法中进行处理.
You need to handle that in form_valid()
method of the view.
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)
更多参考,请参见 查看全文