Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
Dynamically generated form choices in Django

I have a form for submitting new articles in Django, in this form you're allowed to place the post into a 'user_group', just a many many relationship between groups and users. However, you're only allowed to add it into groups you belong to. Using the init function of the form class i can pass in an extra field, and I do get the correct choices I need, however on submit I get an error ''QueryDict' object has no attribute 'all''

I'm not sure whats going wrong, heres my form:

class PostForm(BaseModelForm):
new_image = forms.ImageField(required=False)
#GROUPS = user.groups.all()
#group = forms.ChoiceField(choices=GROUPS, required=False )

def __init__(self,groups, *args, **kwargs):
    super(PostForm, self).__init__(*args, **kwargs)
    self.fields['group'].queryset = groups

    class Meta:
        model = Post
        fields = ('title','category', 'group', 'text', 'description', 'style')

        help_texts = {
            'group': _('Do you want this published under your account or a group?')
        }

and heres the view where the error is being thrown:

@login_required
def post_new(request):
    if request.method == "POST":
        form = PostForm(request.POST, request.FILES)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = PostForm(groups=request.user.user_groups.all())
    return render(request, 'blog/post_edit.html', {'form': form})

This line:

form = PostForm(groups=request.user.user_groups.all())

Is where I pass in the choices for groups, which does give you the correct choices. The fact that the error happens on submitting make me think its an error in how the view processes it, but Im not sure where.

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You need to pass groups to the form for GET and POST requests. At the moment you're only doing it for GET requests. It should be

if request.method == "POST":
    form = PostForm(request.user.user_groups.all(), request.POST, request.FILES)
    ...
about 4 years ago · Santiago Trujillo Report

0

I think you have to query for the groups in the form:

class PostForm(models.ModelForm):
    group = forms.ChoiceField(queryset = None)

    def __init__(self,groups, *args, **kwargs):
        super(PostForm, self).__init__(*args, **kwargs)
        self.fields['group'].queryset = request.user.user_groups.all()

https://docs.djangoproject.com/en/1.11/ref/forms/fields/#fields-which-handle-relationships

It's important to define queryset as None, and in __init__ make the query when the form instance is created.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!