我正在尝试创建一个审核应用,其中模型包含所有问题,选择和评论形式的答案
class AuditQuestion(models.Model):
NOT_INITIATED = 'NI'
IN_PROCESS = 'IP'
COMPELETED = 'C'
STATUS = (
(NOT_INITIATED, 'Not Initiated'),
(IN_PROCESS, 'In Process'),
(COMPELETED, 'Completed'),
)
type_of_room = models.ForeignKey(TypeOfRoom, null=True, blank=True)
question = models.CharField(max_length=512)
status = models.CharField(max_length=2, choices=STATUS, default=NOT_INITIATED)
comments = models.TextField(null=True, blank=True)
def __str__(self):
return self.question
另一个模型包含项目状态,其中包含项目的详细信息,例如建筑物的位置,项目名称,用户和楼层数
class ProjectStatus(models.Model):
floor_no = models.IntegerField(null=True)
project_details = models.ForeignKey(ProjectDetails, null=True)
question = models.ManyToManyField(AuditQuestion, null=True)
def __str__(self):
return str(self.floor_no)
逻辑就像这样......使用ProjectDetails模型创建一个新项目,并为建筑物的每个楼层生成项目状态。每一层都要根据AuditQuestion模型中的问题进行审核,状态和注释将被注册。
我使用了ManyToMany关系,因此项目状态模型可以有多个问题及其状态和注释。
但是,当我试图保存问题时,它不会发生
我的view.py函数如下:
@login_required
def save_question(request):
project_status_id = request.POST.get('project_status_id')
question_id = request.POST.get('question_id')
status = request.POST.get('status')
comments = request.POST.get('comments')
floor_no = request.POST.get('floor_no')
question = get_object_or_404(AuditQuestion, id=question_id)
print (question)
project_status, created = ProjectStatus.objects.get_or_create(id=project_status_id, floor_no=floor_no)
project_status.save()
project_status.question.add(question)
print (project_status.question)
data = {
'status': True,
}
return JsonResponse(data)
第一个print语句正确打印问题对象,但第二个print语句打印 audit.AuditQuestion.None
任何人都可以帮我解决这个问题吗?
答案 0 :(得分:0)