Hi,

I'm using form_for_model & form_for_instance with my specific
BaseForm.

The problem is that depending on some value, I need to display form
with
values limited.

There's a model:

class SomeGroup(models.Model):
    name = models.CharField(primary_key=True)

class Place(models.Model):
    name = models.CharField()
    display_for = models.ManyToManyField(SomeGroup)

class News(models.Model):
    somegroup = models.ForeignKey(SomeGroup)
    content = models.TextField()

For example if group name is "children" the form will display/validate
only values
for which display_for has an appropriate SomeGroup(name=children)
value.

I get gropu name from url.. so it's  a variable in my view.

For example:
/children/news/add/ which means: Add a new news in children group,
when displaying
form display/validate only places available for Somegroup=children.

This can be modified by self.fields['place'].choices = [(p.id, p.name)
for p in Place.objects.filter(display_for__pk=somegroup)

I've read the:
http://groups.google.com/group/django-users/browse_frm/thread/79e4c8fda31388e3/66b7a638054d68bc?lnk=gst&q=base_fields&rnum=1#66b7a638054d68bc
but it doesn't work as I'm getting error that the form got multiple
value for somegropu key.

Here's what I'm doing:

NewsForm = form_for_instance(News, form=NewsOfferForm)

if request.method == "POST":
    form = NewsForm(request.POST, somegroup=somegroup)
    if form.is_valid()
    ....
else:
    form = NewsForm(somegroup=somegroup) # this one works fine, the
problem appears on POST

My forms.py
------------------
class NewsBaseForm(BaseForm):
    def __init__(self, somegroup, *args, **kwargs):
        super(NewsBaseForm, self).__init__(*args, **kwargs)
        self.fields['place'].choices = [(p.id, p.name) for p in
Place.objects.filter(display_for__pk=somegroup)]

It would be the best If I could use the "somegroup" variable name in
my url,
and If I could provide it to the form under the same name so the form
can use it.( I may set it in my view via
form.cleaned_data['somegroup'] as well.) Also I don't want to
display the "somegroup" selector in form (I know how to work out this
problem).

Can anyone please help?

Thanks,

Robert


--~--~---------~--~----~------------~-------~--~----~
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