嗨,我在弄清楚如何解决此错误时遇到了麻烦。我正在创建一个表单,以便能够向正在工作的网站添加新主题。创建表单并尝试使用它提交添加新主题的请求后,我遇到了一个完整性错误,以前从未遇到过。经过研究后,我知道它与外键关系有关,在外键关系中,数据库内部有一个空字段,但我不确定如何导致该根。
这是我的Models.py文件:
<input type="text" name="regDate" value=<%= "\""+cal.getTime()+"\"" %> />
<% cal.add(Calendar.YEAR, 1); %>
<input type="text" name="endDate" value=<%= "\""+cal.getTime()+"\"" %> />
forms.py
from django.db import models
# Create your models here.
class Category(models.Model):
"""A category the user is writing about"""
text = models.CharField(max_length=200)
class Meta:
verbose_name_plural = 'Categories'
def __str__(self):
"""Return a string represtation of the model."""
return self.text
class Topic(models.Model):
"""A topic that is associated with a certain Category"""
category = models.ForeignKey(Category, on_delete=models.CASCADE)
text = models.CharField(max_length=200)
date_added = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name_plural = 'Topics'
def __str__(self):
"""Return string represtation of the model."""
return self.text
class Entry(models.Model):
"""A entry associated with a certain topic"""
topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
text = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name_plural = 'Entries'
def __str__(self):
"""Return string represtation of the model."""
return self.text[:50] + "..."
views.py
from django import forms
from .models import Category, Topic
class CategoryForm(forms.ModelForm):
class Meta:
model = Category
fields = ['text']
labels = {'text': ''}
class TopicForm(forms.ModelForm):
class Meta:
model = Topic
fields = ['text']
labels = {'text': ''}
感谢您帮助我指出正确的方向!
选择类别
将新主题添加到类别