#36128: Document the necessity of adding unique constraints to related models in
intermediary m2m model.
-------------------------------------+-------------------------------------
     Reporter:  Guillaume LEBRETON   |                    Owner:  Clifford
                                     |  Gama
         Type:  Bug                  |                   Status:  new
    Component:  Documentation        |                  Version:  5.1
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Ready for
                                     |  checkin
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Guillaume LEBRETON):

 * resolution:  fixed =>
 * status:  closed => new

Comment:

 I'm sorry to not have taken a look while it was still open, but my point
 was not so about the lack of integrity constraint on the example, but the
 lack of proper validation when there is an integrity constraint.
 I've looked at the patch and it just add a mention about the unique
 constraint, but nothing about the validation.

 If i add this uniqueness constraint, the result of adding a duplicate will
 be an integrity error rather than a validation error, and the hassle is
 writing a complex validation logic like this for a proper validation:

 `admins.py`
 {{{
 class MembersFormset(forms.models.BaseInlineFormSet):

     def clean(self):
         groups = []

         for form in self.forms:
             if form.cleaned_data:
                 groups.append((form.cleaned_data['group'],
 form.cleaned_data['person']))

         duplicated_groups = [x for x in groups if groups.count(x) > 1]
         if duplicated_groups:
             raise ValidationError(
                 'Duplicated values: %(duplicates)s',
                 params={'duplicates': ", ".join(group.__str__() for group
 in set(duplicated_groups))}
             )


 class MembershipInline(admin.TabularInline):
     model = Membership
     extra = 1
     formset = MembersFormset

 }}}

 Maybe i missed something, but i find the updated doc even more misleading
 now, before the example was quite incomplete because there is almost
 always a need for unique constraint in m2m models, but now the provided
 example is not working properly because of failing validation.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36128#comment:10>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/01070195615b25d9-5fc7bfc9-66a7-42a5-86df-ed4279c281f8-000000%40eu-central-1.amazonses.com.

Reply via email to