Python 在表单\u有效后未更新的对象
我有一个Python 在表单\u有效后未更新的对象,python,django,django-models,django-forms,Python,Django,Django Models,Django Forms,我有一个问题和一个答复 创建回复时,我想将相关的问题属性设置为new\u repries=True。此开关应在使用通用CreateView/a表单创建Reply对象后发生 我可以在提交表单后立即更新对象,但是刷新页面会使更新消失。我认为这是因为更新没有保存到数据库中 Models.py class Reply(models.Model): parent_question = models.ForeignKey(Question, null=True) class Question(m
问题
和一个答复
创建回复
时,我想将相关的问题
属性设置为new\u repries=True
。此开关应在使用通用CreateView/a表单创建Reply
对象后发生
我可以在提交表单后立即更新对象,但是刷新页面会使更新消失。我认为这是因为更新没有保存到数据库中
Models.py
class Reply(models.Model):
parent_question = models.ForeignKey(Question, null=True)
class Question(models.Model):
new_replies = models.BooleanField(default=False)
class ReplyCreate(CreateView):
"""
creates a comment object
"""
model = Reply #specify which model can be created here
fields = ['content', ] # which fields can be openly editted
...
def form_valid(self, form):
"""
add associate blog and author to form.
"""
#this is setting the author of the form
form.instance.author = self.request.user.useredus
#associate comment with Question based on passed
parent_question = get_object_or_404(Question, pk= self.kwargs['pk'])
form.instance.parent_question = parent_question
response = super(ReplyCreate, self).form_valid(form)
# PROBLEM: not being saved to dB?
parent_question.new_replies = True # inform the question there are new replies
parent_question.save()
return response
def get_success_url(self):
return reverse('edus:question_detail', kwargs={'pk': self.kwargs['pk'], })
Views.py
class Reply(models.Model):
parent_question = models.ForeignKey(Question, null=True)
class Question(models.Model):
new_replies = models.BooleanField(default=False)
class ReplyCreate(CreateView):
"""
creates a comment object
"""
model = Reply #specify which model can be created here
fields = ['content', ] # which fields can be openly editted
...
def form_valid(self, form):
"""
add associate blog and author to form.
"""
#this is setting the author of the form
form.instance.author = self.request.user.useredus
#associate comment with Question based on passed
parent_question = get_object_or_404(Question, pk= self.kwargs['pk'])
form.instance.parent_question = parent_question
response = super(ReplyCreate, self).form_valid(form)
# PROBLEM: not being saved to dB?
parent_question.new_replies = True # inform the question there are new replies
parent_question.save()
return response
def get_success_url(self):
return reverse('edus:question_detail', kwargs={'pk': self.kwargs['pk'], })
每个问题有两个用户?这应该保存在问题模型中。然后调用此属性,在表单验证中可以更新第二个用户配置文件,因为您知道第二个用户是谁。不,一个问题只有一个用户。