Hi Jon,

On 07/02/2014 09:06 PM, Jon Dufresne wrote:
> I'm reporting this to the developers list as I feel this shows a
> shortfall in (to me) expected behavior. I'm not sure this is exactly a
> bug or simply a use case the unique validation wasn't designed for.
> 
> Basically, sometimes I want to create model formsets that use a
> limited number of model fields. These model fields may have a unique
> constraint using unique_together. The other field (or fields) of
> unique together may not be included in the formset. Upon validating
> the form, unique_together fields are only checked if all fields are
> contained in the form. This means, my unique fields will not be
> validated automatically. To achieve the validation I usually have to
> copy parts of Django form validation, missing out on DRY.
> 
> I think one solution would be for model formsets to have a "static" or
> "constant" fields argument. This could be a single value per field
> that all forms in the formset would share for a particular set of
> fields. These fields would not be a part of the rendered form, but
> could be used for validating the forms and creating new models
> instances. Thoughts?

I agree this is a problem that should be fixed; it is tracked in
https://code.djangoproject.com/ticket/13091 and there is already a
history of proposals for fixing it.

I think your proposed fix is a new one, though. It's less flexible than
the context-manager API I had proposed at one time [1], but it may cover
the 80% case adequately, and has the major advantage of not requiring
any changes in the code that checks the validity of the form.

There is already a way to get "default" values for excluded fields into
a ModelForm: pass it an unsaved "instance" at form instantiation, with
those fields set on it. So it feels odd to me to add a new way to pass
in such values. But passing in an instance doesn't trigger full-model
validation, and making it do that could break existing code, so we'd
still need some way to flag that you want that behavior.

Hmm...

Carl

 [1]
https://groups.google.com/d/topic/django-developers/PT17cgDSOw8/discussion

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to