[Django] #29631: Feature Request: models.UniqueBooleanField()

3 views
Skip to first unread message

Django

unread,
Aug 2, 2018, 10:40:04 PM8/2/18
to django-...@googlegroups.com
#29631: Feature Request: models.UniqueBooleanField()
-------------------------------------+-------------------------------------
Reporter: Michael | Owner: nobody
Type: New | Status: new
feature |
Component: Database | Version: 2.1
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Imagine the schema for a tweet on Twitter:

{{{
class Tweet(models.Model):
content = models.Charfield(max_length=280)
user = models.ForeignKey(User)
pinned = models.UniqueBooleanField(default=False, unique_with='user')
}}}

The user pins a tweet and behind the scenes `UniqueBooleanField` would set
the previous tweet's pinned field to False (if any) and set the current
one to True. This has tons of other use cases, including: the default
group for a user and anything where there can only be one "default" given
a `unique_with` property.

Is this a dumb idea? Is there already a better, more Django-idiomatic way?
All feedback is appreciated.

Thanks,

Michael

--
Ticket URL: <https://code.djangoproject.com/ticket/29631>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 3, 2018, 4:43:47 AM8/3/18
to django-...@googlegroups.com
#29631: Feature Request: models.UniqueBooleanField()
-------------------------------------+-------------------------------------
Reporter: Michael | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

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


Comment:

Hi iMerica,

Thanks for the input. Here are my thoughts:

1. There's nothing to stop you creating
[https://docs.djangoproject.com/en/2.0/howto/custom-model-fields/ a custom
field] for this. I'm not sure it's something we'd add to Django itself —
It would be a bit too special case — but if you had an implementation, we
could certainly look.
2. Assuming I've understood the use-case correctly, you could model this
with a nullable `OneToOneField`. (Each user can pin at most one of their
own tweets.) When pinning you'd just need to clear any existing pin before
saving. Then you'd just access via something like `user.pinned_tweet`.
3. I'd probably be inclined to store the `pinned_tweet` for each user in a
third table. (Do you have a profile model or such — maybe there — or just
a two-field lookup model if not.) Again here the foreign key to `User`
would be `unique` (or, the same thing, one-to-one).

Hopefully that gives you options. If you need further help please post to
[https://groups.google.com/d/forum/django-users the Django Users mailing
list].

If you come up with an implementation for your field that you think might
be good for inclusion, create a Pull Request on GitHub and re-open this
issue or post to [django-developers mailing archive the Django Developers
mailing list] to get feedback.

Thanks again.

--
Ticket URL: <https://code.djangoproject.com/ticket/29631#comment:1>

Django

unread,
Aug 3, 2018, 10:47:44 AM8/3/18
to django-...@googlegroups.com
#29631: Feature Request: models.UniqueBooleanField()
-------------------------------------+-------------------------------------
Reporter: Michael | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

I'd add that the usual way to implement such a feature is using a partial
composite unique index which is something #29547 is trying to add support
for.

In the mean time you can enforce it at the database level using a
`RunSQLOperation`.

{{{#!sql
CREATE UNIQUE INDEX pinned_tweet ON tweet (user_id, pinned) WHERE pinned =
true;
}}}

If your database doesn't support partial index a workaround would be to
declare your `pinned` field as `NULL`able and use `NULL` as the ''falsey''
value as it will be ignored in unique constraints.

--
Ticket URL: <https://code.djangoproject.com/ticket/29631#comment:2>

Reply all
Reply to author
Forward
0 new messages