“user_id”列中的空值违反了Django 1.9中的非空约束

时间:2016-02-19 20:55:17

标签: django django-models django-forms django-templates django-views

我已经用尽了所有尝试为此设置解决方案的途径,但我目前对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)])

故障单视图(仅限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()

1 个答案:

答案 0 :(得分:4)

您似乎需要在TicketCreate函数的form_valid课程中添加以下内容:

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

让我知道这是否有效!