On 02/28/2011 12:50 PM, Ofer Koren wrote:
> I'll use my previous example again.
> Basically it fails because of the initial value set on the InstrumentForm.
> The formset thinks the empty form as /changed/ (as if the Instrument
> field was /modified /to the /initial/ value), and because some other
> fields are required and not filled, fails validation.
> I would expect supplying an 'initial' value to a field wouldn't cause
> the form to be regarded as modified.
> Any ideas what I might be doing wrong?
>
Set initial during formset instantiation:
http://docs.djangoproject.com/en/dev/topics/forms/formsets/#using-initial-data-with-a-formset
Cheers
--
Meir Kriheli
>
> # (models.py):
>
> class InstrumentType(Model):
> name = CharField(...)
>
>
> class Instrument(Model):
>
> auth_user = ForeignKey(User)
> instrument = ForeignKey(InstrumentType)
> model = CharField(required=True)
>
> # (forms.py):
>
> class InstrumentForm(django.forms.ModelForm):
>
> *
> instrument = forms.ModelChoiceField(
> queryset=Instrument.objects.all(),
> initial=Instrument.objects.get(name='guitar')
> )
> *
> www.mrbroken.com <http://www.mrbroken.com>
>
> --
> You received this message because you are subscribed to the Google
> Groups "PyWeb-IL" group.
> To post to this group, send email to pywe...@googlegroups.com.
> To unsubscribe from this group, send email to
> pyweb-il+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/pyweb-il?hl=en.
I think you misunderstood initial data: Forms with initial data are not
considered "empty unless something changed". An empty form is only one where
all fields are empty.
You can get the behavior you want by overriding the formset's clean() method:
http://docs.djangoproject.com/en/dev/topics/forms/formsets/#custom-formset-
validation
Shai.
On Sunday 06 March 2011, Ofer Koren wrote:
> >> > # (views.py):
> >> >
> >> > InstrumentsFormSet = modelformset_factory(
> >> >
> >> > Instrument,
> >> > can_delete=True,
> >> > form=InstrumentForm)
> >> >
> >> > def manage_instruments_view(request):
> >> > user_instruments =
> >> > Instrument.objects.filter(auth_user=request.user)
> >> >
> >> > if request.method == "GET":
> >> > formset = InstrumentsFormSet(queryset=user_instruments)
> >> >
> >> > elif request.method == "POST":
> >> > formset = InstrumentsFormSet(request.POST,
> >> >
> >> > queryset=user_instruments)
Why are you passing in the queryset here?
Shai.
--