通过视图提交表单时获取“ NOT NULL约束”。通过/ admin

时间:2019-01-11 23:02:31

标签: python django python-3.x

我正在创建一个简单的ratemyteacher / prof克隆。问题是,当尝试通过我的视图添加Review对象时,我得到NOT NULL constraint failed: rate_review.review_id(应用程序名称为rate)。通过/admin添加时,效果很好。另外,添加其他模型也可以。

这是发生它的视图:

def add_review(request, teacher_id):
    form = ReviewForm()
    if request.method == 'POST':
        form = ReviewForm(request.POST)
        if form.is_valid():
            ip = request.META.get('HTTP_CF_CONNECTING_IP')
            if ip is None:
                ip = request.META.get('REMOTE_ADDR')
            form.customSave()
            messages.success(request, 'Review added.')
        else:
            form = ReviewForm(request.POST)
            return render(request, 'rate/add_review.html', {'form': form})
    return render(request, 'rate/add_review.html', {'form': form})

这是表格(被截断以排除load)。我正在使用this来呈现表单:

<form method="POST">{% csrf_token %}
            <div class="field">
                <label class="label">Stars (whole numbers only)</label>
                <div class="control">
                    {% render_field form.stars class+="input" %}
                </div>
            </div>
            <div class="field">
                <label class="label">Review subject</label>
                <div class="control">
                    {% render_field form.subject class+="input" %}
                </div>
            </div>

            <div class="field">
                <label class="label">Review text</label>
                <div class="control">
                    {% render_field form.text class+="textarea" placeholder="Review text" rows="10" %}
                </div>
            </div>

            <div class="field">
                <label class="label">Username</label>
                <div class="control">
                    {% render_field form.author class+="input" placeholder="eg. ReviewerMan21, John Smith" %}
                </div>
            </div>


            <button type="submit" class="button">Add review</button>
        </form>

我的models.py:

class Teacher(models.Model):
    grade = models.IntegerField()
    name = models.CharField(max_length=35)
    subject = models.CharField(max_length=50)
    ip = models.CharField(max_length=14)
    approved = models.BooleanField(null=True, blank=True)

class Review(models.Model):
    teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE)
    text = models.TextField()
    subject = models.CharField(max_length=120)
    author = models.CharField(max_length=35)
    ip = models.CharField(max_length=14)
    date = models.DateTimeField(auto_now_add=True)
    stars = models.PositiveSmallIntegerField()

我的forms.py:

from django import forms
from .models import Teacher, Review, Grade
from django.core import validators

class ReviewForm(forms.ModelForm):
    class Meta:
        model = Review
        fields = ('subject', 'text', 'stars', 'author')

    def customSave(self, ip):
        lv = self.save(commit=False)
        lv.ip = ip
        lv.save()
        return lv

class TeacherForm(forms.ModelForm):
    class Meta:
        model = Teacher
        fields = ('subject', 'name', 'grade')

    def customSave(self, ip):
        lv = self.save(commit=False)
        lv.ip = ip
        lv.save()
        return lv

我尝试过的事情

  • 重置/刷新数据库
  • 注释ipauthorstars
  • blank=True, null=True添加到ipstarsauthor
  • 迁移数据库

编辑:我已通过将customSave中的ReviewForm更改为此(然后通过我的观点传递teacher变量,来解决(由于下面的回答):

    def customSave(self, ip, teacher):
        lv = self.save(commit=False)
        lv.ip = ip
        lv.teacher = teacher
        lv.save()
        return lv

1 个答案:

答案 0 :(得分:1)

您的错误表明review_idNonenull)。我注意到您在Teacher模型上将Review的ForeignKey字段命名为review而不是teacher,这表明review_id确实属于{{ 1}}对象。

我注意到您的视图Teacher方法的第一行,您获得了一个add_review对象,但从未对其进行任何操作。您是否打算将您新创建的评论中的老师(“评论”字段)设置为此教师实例?