python - Django 模型和 View

标签 python django

我正在尝试创建我的第一个项目。喜欢 Stack Overflow 上特定主题的问题和答案。我通过 AbstractBaseUser 创建自己的用户。现在我想创建问答模型。

我的模型:

class Question(models.Model):

text = models.TextField()
title = models.CharField(max_length=200)
date = models.DateTimeField(default=datetime.datetime.now)
author = models.ForeignKey(CustomUser)
def __str__(self):
    return self.title

类答案(models.Model):

text = models.TextField()
date = models.DateTimeField(default=datetime.datetime.now)
likes = models.IntegerField(default=0)
author = models.ForeignKey(CustomUser)
question = models.ForeignKey(Question)

类标签(models.Model):

TYPE_OF_TAGS = (
    ('First tag', 'First'),
    ('Second tag', 'Second'),
    ('Third tag', 'Third'),
    )
type_of_tag = models.CharField(max_length=12, choices=TYPE_OF_TAGS, blank=True)
question = models.ManyToManyField(Question)

我的观点:

def question(request):

args = {}
args.update(csrf(request))
args['form'] = QuestionForm()
args['profile'] = CustomUser.objects.filter(pk = request.user.pk)
args['author'] = Question()

if request.POST:
    args['form'] = QuestionForm(request.POST)
    obj = args['form'].save(commit=False)
    obj.save()
    args['author'].author = args['profile']
    args['author'].save()
    return redirect(reverse(question))

return render(request, 'questions.html', args)

我创建了第一个答案 View ,但发送表单时出现错误:

null value in column "author_id" violates not-null constraint

我想我走错了方向。我有糟糕的模型和糟糕的观点。为什么我会出现此错误?我的模型有什么问题?

最佳答案

如果您的业务规则不允许问题的作者为空,则必须在保存之前将作者值设置为您的 obj。 像这样的东西:

def question(request):

    args = {}
    args.update(csrf(request))
    args['form'] = QuestionForm()
    args['profile'] = CustomUser.objects.filter(pk = request.user.pk)

    if request.POST:
        args['form'] = QuestionForm(request.POST)
        obj = args['form'].save(commit=False)
        obj.author = args['profile']
        obj.save()
        return redirect('your_redirect_url')

    return render(request, 'questions.html', args)

您还有另一个选择: 您可以将以下内容的初始字典传递给 QuestionForm:

{
    'author' : CustomUser.objects.filter(pk = request.user.pk)
}

类似于:

def question(request):

    args = {}
    args.update(csrf(request))
    args['form'] = QuestionForm(initial={
        'author': CustomUser.objects.filter(pk = request.user.pk)    
    })

    if request.POST:
        args['form'] = QuestionForm(request.POST)
        obj = args['form'].save()
        return redirect('your_redirect_url')

    return render(request, 'questions.html', args)

注意:在您的 questions.html 模板中渲染作者值的隐藏字段非常重要,因此它包含在 POST 数据中。

关于python - Django 模型和 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32744188/

相关文章:

Python 突出显示文本字段中的 searchValue

在第三方网站的 iframe 中访问时,未为子域设置 Django csrf cookie

python - 使用外键添加两行 - SQL

python - 如何在 PyOpenGL 中面向对象进行击退?

python - 有没有办法更改函数外部定义的变量或函数

python - 通过重复索引删除行

python - 将字符串列表转换为数值向量以计算汉明距离

python - 了解欧几里得算法在 Python 中的 GCF 实现

python - 使用 Django 交换数据库中的 2 个对象的表单

python - Django 将 sqlite3 db 迁移到 postgres 查询整数 : "none" error 的无效输入语法