-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Model form validation in collections: instance attribute not set #55
Comments
Please retry with this branch: https://github.com/jrief/django-formset/tree/releases/1.0.x There the problem with validating unique indices is solved. |
Yes, that works for fields with I think I noticed another related issue: collections for related models do not know their parent instance during validation. Inline formsets are instantiated with the parent instance of the relation, and that instance is passed on to the model forms as data for the InlineForeignKeyField. This way, the parent id is available for validation - most notably This does not happen for collections for related models. Currently when adding a new related object that would violate unique constraints, the validation for |
Please handle separate issues as such, and do not use an existing one for adding new features, otherwise we get confused. |
Collections create a copy of the underlying form for validation via
HolderMixin.replicate
. Butreplicate
does not assign a model instance to model forms, leading to issues with validation.When creating a new object, this isn't an issue. But when editing an existing object, the form's
instance
attribute must be set to that object for uniqueness validation to work properly.To clarify, this only affects validation, more specifically uniqueness validation, because the unique checks take the instance's state and primary key into account: model._perform_unique_checks. Note that including the primary key field in the form is not enough: the form's model instance was instantiated as new object and it will remain so -
instance._state.adding
remains True until the object was saved.To set the correct instance, I suggest trying to fetch the existing object from the database when the form is being replicated. I can whip up a PR if you want.
The text was updated successfully, but these errors were encountered: