gpt4 book ai didi

python - id 该字段是必需的 - Django 表单集中的错误

转载 作者:太空宇宙 更新时间:2023-11-03 20:20:07 27 4
gpt4 key购买 nike

我正在使用 Django 2.2。我的模型是

class Group(models.Model):
group_name = models.CharField(max_length=100)
def __str__(self):
return self.group_name
class Category(models.Model):
category_name = models.CharField(max_length=50)
def __str__(self):
return self.category_name
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
board = models.CharField(choices=board_options,max_length=1,null=True)
group = models.ForeignKey(Group,on_delete=models.CASCADE,null=True)

class Subject(models.Model):
subject_name = models.CharField(max_length=50)
subject_category = models.ForeignKey(Category, on_delete=models.CASCADE)
subject_photo = models.ImageField(null=True,blank=True)
def __str__(self):
return self.subject_name

class Subject_Assignment(models.Model):
board = models.CharField(choices=board_options,max_length=1,null=True)
group = models.ForeignKey(Group,on_delete=models.CASCADE,null=True)
Subject = models.ForeignKey(Subject, on_delete=models.CASCADE)

class Mark_Survey(models.Model):
survey_name = models.CharField(max_length=50)
opens_at = models.DateField()
ends_at = models.DateField()

class Survey_Mark(models.Model):
mark_survey = models.ForeignKey(Mark_Survey,on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
Subject = models.ForeignKey(Subject, on_delete=models.CASCADE)
marks = models.IntegerField()

这里正在为 Survey_Mark 创建一个表单集,我的表单应该获得所有主题的标记。通过使用Subject_Assignment,我可以获取特定用户的所有主题。以下 View 适用于get方法,但在post方法中抛出错误。

def postsurvey(request,pk):
#post = get_object_or_404(Mark_Survey, pk=pk)
SurveyFormSet = modelformset_factory(Survey_Mark, fields=('marks','Subject'),extra=0)
if request.method == "POST":
formset = SurveyFormSet(request.POST,request.FILES)
print(formset.data)
print(formset.errors)

if formset.is_valid():
post = formset.save(commit=False)
post.mark_survey=pk
post.user=request.user
post.save()
html = "<html><body>Success</body></html>" % now
return HttpResponse(html)
else:
print("failure")
html = "<html><body>failure </body></html>"
return HttpResponse(html)

else:
user=request.user
profile = get_object_or_404(Profile,user_id=user.id)
formset = SurveyFormSet(queryset=Subject_Assignment.objects.filter(Q(board=profile.board) & Q(group=profile.group)))
return render(request, 'subject_assignment_detail.html', {'formset': formset})

我收到 id 该字段为必填错误。如果我在模板中使用了 {{ hide }} 那么我会选择一个有效的选择。该选择不是 post 方法中可用的选择之一 错误。我想以单一形式获取所有主题的分数并将其保存在相应的表中。我的模板是

<form method="post">
{% csrf_token %}
{{ formset.management_form }}


{% for form in formset %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{{form}}

{% endfor %}

<input type="submit" value="OK">
</form>

最佳答案

解决方案是为每个主题实例提供一个标记值。

user=request.user
profile = get_object_or_404(Profile,user_id=user.id)
subjects = Subject_Assignment.objects.filter(
Q(board=profile.board) & Q(group=profile.group)
)
SurveyFormSet = modelformset_factory(
Survey_Mark,
fields=('marks','Subject'),
extra=len(subjects)
)

显示的空表单数量由 extra 参数控制,因此我为 extra 指定了 Subject 的数量参数并传递初始数据。

formset = SurveyFormSet(
initial=[{'marks':'','Subject':x.id} for x in subjects]
)

关于python - id 该字段是必需的 - Django 表单集中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58217972/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com