No form validation is implemented for UniqueConstraint(condition) yet as 
that would require
a non-trivial refactor of how it's performed. It was discussed during the 
feature development[0].

I'd suggest you override you form or your model's clean() method to perform 
the validation
yourself until built-in support is added.

Submitting a new Trac ticket so we don't loose track of the issue would 
also be appreciated.

Cheers,
Simon

[0] https://github.com/django/django/pull/10796#discussion_r244216763

Le mardi 9 avril 2019 07:29:53 UTC-4, Ryan Jarvis a écrit :
>
> Hey there,
>
> I'm trying out the new UniqueConstraint functionality in Django 2.2 and 
> seem to be misunderstanding something.  When adding a Constraint to the 
> model I am getting an uncaught IntegrityError in the admin.  
> I've got the following sample code:
>
> *models.py*
>
> class Seminar(models.Model):
>     seminar_id = models.CharField(max_length=255, unique=True)
>     members = models.ManyToManyField(User, through='SeminarRole', 
> related_name="studies")
>
> class SeminarRole(models.Model):
>     LEAD = 1  # Only 1 Lead permitted per seminar
>     SUPPORT = 2
>     ROLE_CHOICES = (
>         (LEAD, 'Lead'),
>         (SUPPORT, 'Support'),
>     )
>
>     user = models.ForeignKey(User, related_name='seminar_roles', 
> on_delete=models.CASCADE)
>     seminar = models.ForeignKey('seminar', related_name='roles', 
> on_delete=models.CASCADE)
>
>     role = models.IntegerField(choices=ROLE_CHOICES)
>
>     class Meta:
>         constraints = [
>             models.UniqueConstraint(fields=['seminar'], 
> condition=Q(role=1), name="only_one_lead"),
>         ]
>
>
>
> For the code above in the Django Admin I can successfully add a 
> SeminarRole with User1 as the Lead for SeminarA, User2 as the Lead for 
> SeminarB, and User1 as Support for SeminarB but if I try and add User2 as 
> another Lead to SeminarA it gives me an Exception.  Should I be seeing the 
> Django Admin catch this before hand?
>
> IntegrityError at /admin/study_management/seminarrole/add/
> duplicate key value violates unique constraint "only_one_lead"
> DETAIL:  Key (seminar_id)=(1) already exists.
>
>
> I'm on Django 2.2, Python 3.7 and Postgres 11.2
>
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/12fba4e5-7f07-4491-afb6-b969e73252da%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to