简体   繁体   English

我不断收到此错误 NOT NULL constraint failed: lists_listcreate.user_id

[英]I keep getting this error NOT NULL constraint failed: lists_listcreate.user_id

I am trying to make a site where users can make lists and then view those lists.我正在尝试创建一个网站,用户可以在其中制作列表,然后查看这些列表。 How my site is set up is the user creates the list and gives it a name and then they move on to the next page where they can add items to their list.我的站点的设置方式是用户创建列表并为其命名,然后他们转到下一页,在那里他们可以将项目添加到他们的列表中。 The login prosses works fine and you can go to the creat list page just fine.登录过程工作正常,您可以 go 到创建列表页面就好了。 however, when you hit next on the create list page and try to go to the page where you go to the page where you add items to the list it gives you this error: IntegrityError at /your_lists/new_list/ NOT NULL constraint failed: lists_listcreate.user_id但是,当您在创建列表页面上点击下一步并尝试 go 到您所在的页面 go 到您将项目添加到列表的页面时,它会给您这个错误: IntegrityError at /your_lists/new_list/ NOT NULL constraint failed: lists_listcreate 。用户身份

Views:意见:

from django.shortcuts import render
from .models import List, ListIcon, ListCreate
from django.views.generic import TemplateView, CreateView
from django.views.generic.edit import FormView
from django.contrib.auth.mixins import LoginRequiredMixin
from . import models
from .forms import AddListItemForm, CreatListForm
from django.http import HttpResponseRedirect

# Create your views here.

class CreateList(CreateView, LoginRequiredMixin):
    model = ListCreate
    form_class = CreatListForm
    template_name ="lists/list_form.html"
    success_url = '/add_item/'

class AddListItem(CreateView, LoginRequiredMixin):
    model = List
    form_class = AddListItemForm
    template_name = "lists/AddListItem.html"
    def index(requst):
        list = CreateList.objects.all()
        if request.method == "POST":
            if "add_one_more" in requst.POST:
                list.save()
                return redirect("/")
            if "done" in requst.POST:
                return redirect("/lists/your_lists/")

class ViewAllLists(TemplateView, LoginRequiredMixin):
    model = ListIcon
    template_name = 'lists/EveryList.html'
    def get_user_list_icons(self, **kwargs):
        if request.user.is_authenticated():
            lists_by_user = ListIcon.objects.filter(user=request.user)
        return render(lists_by_user)

Models:楷模:

    from django.db import models
from django.contrib.auth import get_user_model
# Create your models here.
User = get_user_model()

class List(models.Model):
    user = models.ForeignKey(User, related_name='lists_ListIcon_user', on_delete=models.CASCADE)
    list_item = models.CharField(max_length=300, blank=True)

#class AddListItem(models.Model):

class ListCreate(models.Model):
    user = models.ForeignKey(User, related_name='lists_ListIcon_users', on_delete=models.CASCADE)
    list_names = models.CharField(max_length=100, default='Enter list name here')

class ListIcon(models.Model):
    user = models.ForeignKey(User, related_name='users', on_delete=models.CASCADE)
    list_title = models.ForeignKey(ListCreate, related_name='list_name', on_delete=models.CASCADE)

enter code here

You are getting this error because you are not passing a user when creating a list instance overriding the form_valid method should fix like this.您收到此错误是因为您在创建覆盖 form_valid 方法的列表实例时没有传递用户应该像这样修复。

class AddListItem(CreateView, LoginRequiredMixin):
model = List
form_class = AddListItemForm
template_name = "lists/AddListItem.html"
def index(requst):
    list = CreateList.objects.all()
    if request.method == "POST":
        if "add_one_more" in requst.POST:
            list.save()
            return redirect("/")
        if "done" in requst.POST:
            return redirect("/lists/your_lists/")
        # add this 
    

def form_valid(self, form):
    form.instance.user = self.request.user # if you want it to be the request.user
    return super().form_valid(form)

暂无
暂无

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

相关问题 Django:“NOT NULL 约束失败:users_investment.user_id”错误 - Django: "NOT NULL constraint failed: users_investment.user_id" Error 为什么我会收到此错误? 唯一约束失败:app1_comment.user_id - Why am I getting this error? UNIQUE constraint failed: app1_comment.user_id Django:“ NOT NULL约束失败”。user_id - Django: “NOT NULL constraint failed” .user_id 我收到此错误 NOT NULL 约束失败:accounts_user.password - I got this error NOT NULL constraint failed: accounts_user.password 为什么我在没有设置唯一约束的数据上不断收到错误“唯一约束失败”? - why do I keep getting the error 'Unique constraint failed' on data I haven't put a Unique constraint on? /listing/1/ NOT NULL 约束处​​的 IntegrityError 失败:auctions_comments.user_id。 我正在尝试保存评论并需要帮助解决此错误 - IntegrityError at /listing/1/ NOT NULL constraint failed: auctions_comments.user_id. I am trying to save comments and need help resolving this error 获取数据库错误,NOT NULL 约束失败 - Getting database error, NOT NULL constraint failed 我在 /post/new/ NOT NULL 约束失败处收到 IntegrityError:blog_post.author_id - I'm getting an IntegrityError at /post/new/ NOT NULL constraint failed: blog_post.author_id 获取 NOT NULL 约束失败:DRF 中的locations_location.city_id - Getting NOT NULL constraint failed: locations_location.city_id in DRF NOT NULL约束失败:users_userprofile.user_id - NOT NULL constraint failed: users_userprofile.user_id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM