There are multiple situations where such a system is useful: a computer
can have multiple codes of different types, but for each code type the
code value is unique. A room in hotel can have single active reservation
per day.
The proposed patch adds a ReverseUnique(to_model, field_of_to_model,
filters) model field. For examples see the branch
https://github.com/akaariai/django/compare/reverse_unique, the test models
and tests itself contain some example of why this would be useful.
A more generic but a bit harder to implement feature is allowing
annotation of reverse unique values. There the idea would be that you
could do:
{{{
qs.annotate(translation_fi=ModelAnnotation(ArticleTranslation,
through_field='article_translations', filter=Q(lang='fi')))
}}}
then you could use translation_fi like any relation. I think model level
definition of the same thing will be useful in many cases (consider the
active_translation case), so there is room for both model field
implementation and annotation based implementation.
I will likely push the needed ORM & related.py changes into 1.6 but leave
other parts out. Pushing the changes to the new internal related field API
seems OK to me.
--
Ticket URL: <https://code.djangoproject.com/ticket/20611>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* stage: Unreviewed => Accepted
Comment:
Accepting my own ticket - I need it :) I will see if I can get the ORM
changes still to 1.6.
--
Ticket URL: <https://code.djangoproject.com/ticket/20611#comment:1>
Comment (by akaariai):
The ORM changes are really minor, and to a new API, so I will push these
to both 1.6 and master. After that the ReverseUnique field can be
implemented outside Django (although internal APIs must be used).
--
Ticket URL: <https://code.djangoproject.com/ticket/20611#comment:2>
Comment (by Anssi Kääriäinen <akaariai@…>):
In [changeset:"6b4967e88368934dbbb1f289c790ab813fa59c72"]:
{{{
#!CommitTicketReference repository=""
revision="6b4967e88368934dbbb1f289c790ab813fa59c72"
Minor change to get_extra_descriptor_filter()
Refs #20611.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/20611#comment:3>
Comment (by Anssi Kääriäinen <akaariai@…>):
In [changeset:"43f1d51b4ba3ab2c0223928904e1d24c0571da8d"]:
{{{
#!CommitTicketReference repository=""
revision="43f1d51b4ba3ab2c0223928904e1d24c0571da8d"
[1.6.x] Minor change to get_extra_descriptor_filter()
Refs #20611. Backpatch of 6b4967e88368934dbbb1f289c790ab813fa59c72.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/20611#comment:4>
* stage: Accepted => Someday/Maybe
Comment:
For those interested, an implementation exists at
https://github.com/akaariai/django-reverse-unique/, lets see if this type
of field will be useful enough for Django inclusion.
--
Ticket URL: <https://code.djangoproject.com/ticket/20611#comment:5>
* status: new => closed
* resolution: => duplicate
Comment:
Duplicate of [https://code.djangoproject.com/ticket/29789 #29789] and
[https://code.djangoproject.com/ticket/27332 #27332].
--
Ticket URL: <https://code.djangoproject.com/ticket/20611#comment:6>