I'm struggling with a newforms issue. Specifically I'm trying to pre-
select an option in a ModelChoiceField and I cannot figure out how
this can be accomplished. Here are the code snippets:

    class Account(models.Model):
        title = models.CharField(maxlength=30, unique=True,
db_index=True, blank=False)
        administrator = models.ForeignKey(User,
related_name="admin_accounts", blank=False)
        users = models.ManyToManyField(User, blank=True, null=True)

    class Project(models.Model):
        account = models.ForeignKey(Account, blank=False)
        title = models.CharField(maxlength=50, blank=False)

    def add_project(request, acct=None):
        # get new Project form class
        FormClass = forms.models.form_for_model(Project)

        # get accounts associated with this user
        FormClass.base_fields['account'] = \
            forms.ModelChoiceField
(queryset=Account.objects.filter(users__username=request.user.username))

        if acct:
            # if the specified account is valid, set it as the
"selection" in the choice field
            try:
                account = Account.objects.get(id=acct)
            except Account.DoesNotExist:
                pass
            else:
                # set default choice here?
                pass

        if request.method == 'POST':
            form = FormClass(request.POST)
            if form.is_valid():
                form.save()
                return HttpResponseRedirect("/webtools/project/")
        else:
            form = FormClass()

        return render_to_response('add_project.html', {'form': form})


Before rendering the form I want to pre-select the option in the
'account' choice field that corresponds to this account. Presumably
I'll need to look at the options as created by the ModelChoiceField
and match one with account.id or something. However I cannot figure
out how to set the "selection" value before rendering. Any ideas?

Graham


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to