id the field is required - error in Django formset

125 views
Skip to first unread message

madh...@gmail.com

unread,
Oct 3, 2019, 7:32:48 AM10/3/19
to django-d...@googlegroups.com
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,

Adam Johnson

unread,
Oct 3, 2019, 7:44:44 AM10/3/19
to django-d...@googlegroups.com
Hi!

I think you've found the wrong mailing list for this post. This mailing list is for the development of Django itself, not for support using Django. This means the discussions of bugs and features in Django itself, rather than in your code using it. People on this list are unlikely to answer your support query with their limited time and energy. Read more on the mailing lists at https://www.djangoproject.com/community/

For support, please use the NEW Django forum at https://forum.djangoproject.com , django-users mailing list, or IRC #django on Freenode, or a site like Stack Overflow. There are people out there willing to help on those channels, but they might not respond if you don't ask your question well. Stack Overflow's question guide can help you frame it well: https://stackoverflow.com/help/how-to-ask .

Also if you haven't read it, please take a look at Django's Code of Conduct: https://www.djangoproject.com/conduct/ . These are our "ground rules" for working well as a community, and will help you get the most out of Django and our fantastic community.

Thanks for your understanding,

Adam

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAMYHm%2Bd_cd-gaAgcwKjfVq%2BEuNreS0h1sHhcNustj5e2ZbuEww%40mail.gmail.com.


--
Adam
Reply all
Reply to author
Forward
0 new messages