The situation - I have a model and inline formset like:
class ProductImage(models.Model): product = models.ForeignKey('Product') display_order = models.PositiveSmallIntegerField(default=0) class Meta: unique_together = (('product', 'display_order'),) inlineformset_factory( Product, ProductImage, form=ProductImageForm, extra=1)
In the front end I provide a UI to reorder the images for a given product.
The problem:
- Let's say I already saved two images attached to
<Product id=1>
. - These two images have
display_order=0
anddisplay_order=1
respectively. - now I upload a third image, give it
display_order=0
while renumbering the existing images todisplay_order=2
anddisplay_order=1
respectively.
At this point I get a validation error for the unique check.
Looking through Django source code we find this in BaseModelFormSet.save
:
return self.save_existing_objects(commit) + self.save_new_objects(commit)
...ok that looks all good, it will save (update) the existing objects first. So from the perspective of the db there is no conflict in the unique constraints.
The problem is that saving comes after validation, or course, and at the point of validation the new objects will conflict with existing ones in the db.
It seems like BaseModelFormSet
needs some mechanism whereby it could take into account the about-to-be-updated values from its form instances.
Does anyone have a workaround that does not involve pushing the problem onto the user?
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c368d2c7-098e-4e66-94de-0406442ebead%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment