设置排除窗体字段

2024-04-19 03:32:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我在表单域中插入时遇到问题。你知道吗

这是评论型号.py地址:

class UserComment(models.Model):
comment_id=              models.AutoField(primary_key=True)
comment_on_product_id=   models.ForeignKey(Post) 
user_name=               models.CharField(max_length=50)
comment_text=            models.TextField()
comment_timestamp=       models.TimeField(auto_now_add=True,auto_now=True)

还有我的表单.py地址:

class UserCommentForm(forms.ModelForm):
class Meta:
    model=UserComment
    exclude=['comment_on_product_id',]

用户应该看到什么: enter image description here

如您所见,产品id上的注释不应出现在页面上,但我必须在该字段中插入一个值。 在视图中,我的代码如下: . . . 你知道吗

form=UserCommentForm(request.POST or None)
if form.is_valid():
    form.Meta.model.comment_on_product_id= selected_object.product_id
    save_it=form.save(commit=False)
    save_it.save()

但是看起来表单元模型.comment\u on\u product\u id=选定_对象.产品id剂量工作。 错误: 失败行包含(112,null,usernaem,comment text,18:33:39.848621)。你知道吗

为什么为空?我该怎么设置呢?你知道吗


Tags: textpyformidtrue表单autoon