#33137: Decouple Field.unique from select_related
-------------------------------------+-------------------------------------
     Reporter:  Markus Holtermann    |                    Owner:  nobody
         Type:  New feature          |                   Status:  new
    Component:  Database layer       |                  Version:
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

 * cc: Simon Charette (added)
 * stage:  Unreviewed => Accepted


Comment:

 This use case looks very similar to the [https://github.com/akaariai
 /django-reverse-unique django-reverse-unique] project by Anssi which
 didn't require any adjustments to the ORM and properly supported
 `select_related`.


 {{{#!python
 class A(models.Model):
     b = models.ForeignKey('B')
     deleted = models.BooleanField()

     class Meta:
         constraints = [
             UniqueConstraint(fields=['b'], condition=Q(deleted=False)),
         ]

 class B(models.Model):
     a = ReverseUnique(A, filter=Q(deleted=False))
 }}}

 I assume you're trying to create a subclass of `OneToOneField` that allows
 specifying a `condition` and avoids having to add all of this boilerplate?

 {{{#!python
 class A(models.Model):
     b = ConditionalOneToOne('B', condition=Q(deleted=False))
     deleted = models.BooleanField()

 class B(models.Model):
     pass
 }}}

 In all cases I'm not against changing the code to use the
 [https://docs.djangoproject.com/en/3.2/ref/models/fields/#attributes-for-
 fields-with-relations documented] `(many|one)_to_(many|one)` attributes
 instead of `unique` assuming we write a proper regression tests for them.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33137#comment:2>
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 on the web visit 
https://groups.google.com/d/msgid/django-updates/065.6975b945b1e68136e5cf4e3f4c13dc10%40djangoproject.com.

Reply via email to