gpt4 book ai didi

django - "user_id"列中的空值违反了 Django 1.9 中的非空约束

转载 作者:行者123 更新时间:2023-12-02 09:24:56 27 4
gpt4 key购买 nike

我已经用尽了所有途径来尝试为此提供解决方案,但我目前对 Python 和 Django 的了解只能让我到目前为止。

我正在创建一个基本的票务系统,CreateView 过去一直可以工作,直到我创建了一个 Profile 模型,然后将 Ticket 模型分离到它自己的应用程序中。当我重构代码时,已经创建了一些票证,这就是为什么我知道 ListView 可以工作、DeleteView 以及 DetailView 可以工作的原因。 CreateView 一直有效,直到我点击保存按钮。

我的观点和模型如下;我希望有人能帮我解决这个问题。

门票型号

from django.db import models
from django.contrib.auth.models import User
....
from qcapp.models import Profile


class Ticket(models.Model):
# Relations
user = models.ForeignKey(Profile, on_delete=models.CASCADE, related_name="tickets", verbose_name="user")
# Attributes
title = models.CharField(max_length=250, verbose_name="Title", help_text="Enter a Ticket Title")
color = models.CharField(max_length=7,
default="#ffffff",
validators=[RegexValidator("(^#[0-9a-fA-F]{3}$)|(^#[0-9a-fA-F]{6}$)")],
verbose_name="Color",
help_text="Enter the hex color code, like #ccc or #cccccc")
description = models.TextField(max_length=1000)
created_date = models.DateTimeField(default=timezone.now, verbose_name='Created Date')
created_by = models.ForeignKey(User, related_name='created_by_user')
# Attributes
# Object Manager
objects = managers.ProjectManager()

# Meta and String
class Meta:
verbose_name = "Ticket"
verbose_name_plural = "Tickets"
ordering = ("user", "title")
unique_together = ("user", "title")

def __str__(self):
return "%s - %s" % (self.user, self.title)

def get_absolute_url(self):
return reverse('ticket_detail', args=[str(self.id)])

工单 View (仅限 CreateView)

# -*- coding: utf-8 -*-
...
from django.views.generic import CreateView, UpdateView, DeleteView
...
from .models import Ticket


...

class TicketCreate(CreateView):
model = Ticket
template_name = "tickets/ticket_form.html"
fields = ['title', 'description']

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

...

配置文件模型(导入票证模型)

from django.db import models
from django.conf import settings
from django.contrib.auth.models import User
from django.dispatch import receiver
from django.db.models.signals import post_save
from . import managers


class Profile(models.Model):
# Relations
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="profile", verbose_name="user")
# Attributes
interaction = models.PositiveIntegerField(default=0, verbose_name="interaction")
# Attributes
# Object Manager
objects = managers.ProfileManager()

# Custom Properties
@property
def username(self):
return self.user.username

# Methods

# Meta and String
class Meta:
verbose_name = "Profile"
verbose_name_plural = "Profiles"
ordering = ("user",)

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


@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_profile_for_new_user(sender, created, instance, **kwargs):
if created:
profile = Profile(user=instance)
profile.save()

最佳答案

看来您需要将以下内容添加到 form_valid 函数中的 TicketCreate 类中:

form.instance.user = Profile.objects.get(user=self.request.user)

让我知道这是否有效!

关于django - "user_id"列中的空值违反了 Django 1.9 中的非空约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35514857/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com