You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classProduct(SafeDeleteModel):
name=models.CharField(
max_length=100, verbose_name=_("name"), help_text=_("Name of the product")
)
classMeta:
constraints= [
models.UniqueConstraint(
fields=["name"],
condition=Q(deleted=None),
name="product_unique_if_not_deleted",
)
]
However, since we have to add a "condition" on the constraint, Product.has_unique_fields returns False and uniqueness validation is not performed when calling the model .clean method. This means that any model implementing this strategy has to implement custom validation for it
Root cause
The django model has_unique_fields and _perform_unique_checks are overwritten in models.SafeDeleteModel, but they exclude unique constraints with conditions, as shown below:
Solution
A potential solution is to modify the overwritten has_unique_fields and _perform_unique_checks. I am happy to submit a PR for these changes, but what do you think of this change @Gagaro@AndreasBackx@palkeo ?
The text was updated successfully, but these errors were encountered:
Looks like you are on top of it ! 😄
It makes sense to solve this in Django itself. Please let me know if you need help with the changes to django-safedelete, we use it at work and could allocate some time to it if needed
Context
As mentioned in the documentation (https://django-safedelete.readthedocs.io/en/latest/models.html#fields-uniqueness), if we need to define a field uniqueness for non-deleted objects, we have to define a custom constraint such as:
However, since we have to add a "condition" on the constraint,
Product.has_unique_fields
returns False and uniqueness validation is not performed when calling the model.clean
method. This means that any model implementing this strategy has to implement custom validation for itRoot cause
The django model
has_unique_fields
and_perform_unique_checks
are overwritten inmodels.SafeDeleteModel
, but they exclude unique constraints with conditions, as shown below:Solution
A potential solution is to modify the overwritten
has_unique_fields
and_perform_unique_checks
. I am happy to submit a PR for these changes, but what do you think of this change @Gagaro @AndreasBackx @palkeo ?The text was updated successfully, but these errors were encountered: