Thursday, October 3, 2019

id the field is required - error in Django formset


I am using django 2.2. My models are

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()

Here am creating a formset for Survey_Mark and my form should get marks of all Subject. By using Subject_Assignment i can get all the subjects of the particular user.The following view works for the get method but throws error in post method.

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})

Am getting id the field is required error. if i used {{ hidden }} in template then am getting Select a valid choice. That choice is not one of the available choices error in post method.I want to get marks of all the subject in single form and save it in the corresponding table

--
with regards,
B.Madusudhanan,

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMYHm%2BeiAF%3DExBknF3RU49RrRo%2BY%2Bzh-UsrNZ_40avNQB1O%3D3g%40mail.gmail.com.

No comments:

Post a Comment