Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4063058
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:40:39+00:00 2026-05-20T15:40:39+00:00

I have a Django Form class defined likes this in Models : class AccountDetailsForm(forms.Form):

  • 0

I have a Django Form class defined likes this in Models:

class AccountDetailsForm(forms.Form):
    ...
    adminuser = forms.ModelChoiceField(queryset=User.objects.all())

This works OK, but it has some limitations I can’t seem to work around:

(1) I would like to use a filter on the queryset, based on a variable accountid passed to the form, like this:

User.objects.filter(account=accountid)

This can’t work in the model because accountid can’t be passed as a variable, of course.

It follows that the queryset must somehow be defined in the Views, but as far as I can see it’s a required field in the Form class.

(2) I would like to make the default choice of AccountDetailsForm an object in the database, which I can select in the Views like this:

User.objects.filter(account=accountid).filter(primary_user=1)

I’ve tried specifying the adminuser as a default value in the form, (which works with other standard form fields, like CharField):

adminuser = User.objects.filter(account=accountid).filter(primary_user=1)

...

form = AccountDetailsForm({'adminuser': adminuser})
return render_to_response('accounts/edit/accountdetails.html', 
{'form': form, 'account':account})

But no luck.

Should I be using something other than ModelChoiceField given the flexibility I need here?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T15:40:39+00:00Added an answer on May 20, 2026 at 3:40 pm

    Override the init method and accept a new keyword argument

    class AccountDetailsForm(forms.Form):
        ...
        adminuser = forms.ModelChoiceField(queryset=User.objects.all())
        def __init__(self, *args, **kwargs):
            accountid = kwargs.pop('accountid', None)
            super(AccountDetailsForm, self).__init__(*args, **kwargs)
    
            if accountid:
                self.fields['adminuser'].queryset = User.objects.filter(account=accountid)
    
    form = AccountDetailsForm(accountid=3)
    

    You can always just set the choices manually in the view as well.

    form = AccountDetailsForm()
    form.fields['adminuser'].queryset = User.objects.filter(account=accountid)
    

    Be warned: you are not setting default values by passing in a dictionary to a form like in your example.

    You are actually creating a Bound Form, potentially triggering validation and all that jazz.

    To set defaults, use the initials argument.

    form = AccountDetailsForm(initial={'adminuser':'3'})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Weird problem in Django with forms : I have a Form.class defined like this
If I have a Django form such as: class ContactForm(forms.Form): subject = forms.CharField(max_length=100) message
Let's say I've defined this model: class Identifier(models.Model): user = models.ForeignKey(User) key = models.CharField(max_length=64)
When I define a Django form class similar to this: def class MyForm(forms.Form): check
Assume I have a form class SampleClass(forms.Form): name = forms.CharField(max_length=30) age = forms.IntegerField() django_hacker
I have a web report that uses a Django form (new forms) for fields
I have the following Django and Flex code: Django class Author(models.Model): name = models.CharField(max_length=30)
I stumbled over this passage in the Django tutorial : Django models have a
I have a simple model that is defined as: class Article(models.Model): slug = models.SlugField(max_length=50,
My problem is similar to Django Passing Custom Form Parameters to Formset Ive have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.