Today I challenged myself to dive into newforms-admin source code and
try to figure out how to fix the unique_together problem for inline
forms in admin.

I thought I'd share what I found and where I got stuck in case someone
wants to shed some light on the matter. I realize that my knowledge of
Django's inner structure is nowhere near sufficient to be able to find
an elegant solution at the first stab. I took this more as a learning
experience for myself, since someone might even be already coding a
solution.

Anyway, I figured that Django should check the uniqueness of
unique_together fields in the same piece of code as it validates
individual fields. It seems that BaseForm.full_clean() is that place.
The hook for validations not specific to a single field calls
BaseForm.clean(), so maybe that's the best place to put
unique_together validation?

Admin creates forms with the form_for_model() method, and as a
consequence they get a _model attribute which points to the class of
the model which the form represents. So unique_together validation
should be done only if the form's self._model is set and
self._model._meta.unique_together is non-empty. Right?

A query should then be made with the unique_together field and their
corresponding values from the form as filter criteria. But in the case
of inline forms, one of the unique_together fields could be the
foreign key to the parent model inside which the inline form is
displayed. For existing inline objects, the form's self.initial dict
contains the id for the parent form's object, but the empty inline
forms for adding new objects don't include that value.

So the first problem is, how to pass the parent form's object id to
the inline forms when the foreign key is part of unique_together.

This is where I got stuck.

I'd appreciate if someone could show me where I took a wrong turn, or,
if I'm on the right track, give some insight as to how to proceed from
here.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@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-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to