[Django] #30947: Apply data structure best practices to the django.contrib models

20 views
Skip to first unread message

Django

unread,
Nov 2, 2019, 9:58:05 AM11/2/19
to django-...@googlegroups.com
#30947: Apply data structure best practices to the django.contrib models
------------------------------------------------+------------------------
Reporter: Jon Dufresne | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Uncategorized | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------------+------------------------
In PR https://github.com/django/django/pull/11267 an argument is presented
that explains why `list` is often the best choice for Model.Meta data
structures. This landed as
https://github.com/django/django/commit/97d3321e89c8d4434927bdbc308db1ccffa99d3b.

Now change all models in `django.contrib` to follow our own stated best
practices. People often look at existing examples for how to write new
code, so we might as well promote the standards we document.

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

Django

unread,
Nov 3, 2019, 9:48:02 PM11/3/19
to django-...@googlegroups.com
#30947: Apply data structure best practices to the django.contrib models
-------------------------------------+-------------------------------------
Reporter: Jon Dufresne | Owner: Jon
Type: | Dufresne
Cleanup/optimization | Status: assigned
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

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

* status: new => assigned
* cc: Johannes Hoppe (added)
* component: Uncategorized => Documentation
* owner: nobody => Jon Dufresne
* has_patch: 0 => 1
* stage: Unreviewed => Accepted


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

Django

unread,
Nov 4, 2019, 6:26:42 AM11/4/19
to django-...@googlegroups.com
#30947: Apply data structure best practices to the django.contrib models
-------------------------------------+-------------------------------------
Reporter: Jon Dufresne | Owner: Jon
Type: | Dufresne
Cleanup/optimization | Status: assigned
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* needs_better_patch: 0 => 1


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

Django

unread,
Nov 4, 2019, 1:31:05 PM11/4/19
to django-...@googlegroups.com
#30947: Apply data structure best practices to the django.contrib models
-------------------------------------+-------------------------------------
Reporter: Jon Dufresne | Owner: Jon
Type: | Dufresne
Cleanup/optimization | Status: assigned
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

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

* needs_better_patch: 1 => 0


Comment:

Updated PR.

--
Ticket URL: <https://code.djangoproject.com/ticket/30947#comment:3>

Django

unread,
Nov 4, 2019, 4:15:30 PM11/4/19
to django-...@googlegroups.com
#30947: Apply data structure best practices to the django.contrib models
-------------------------------------+-------------------------------------
Reporter: Jon Dufresne | Owner: Jon
Type: | Dufresne
Cleanup/optimization | Status: assigned
Component: Documentation | Version: master
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 CarlosMirdeSouza):

* stage: Accepted => Ready for checkin


Comment:

It`s ok.

--
Ticket URL: <https://code.djangoproject.com/ticket/30947#comment:4>

Django

unread,
Nov 5, 2019, 2:38:26 AM11/5/19
to django-...@googlegroups.com
#30947: Apply data structure best practices to the django.contrib models
-------------------------------------+-------------------------------------
Reporter: Jon Dufresne | Owner: Jon
Type: | Dufresne
Cleanup/optimization | Status: closed
Component: Documentation | Version: master
Severity: Normal | Resolution: fixed

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 Mariusz Felisiak <felisiak.mariusz@…>):

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


Comment:

In [changeset:"b9fe7f9294b1b4fc974c008adeb96e1375cdb0c6" b9fe7f92]:
{{{
#!CommitTicketReference repository=""
revision="b9fe7f9294b1b4fc974c008adeb96e1375cdb0c6"
Fixed #30947 -- Changed tuples to lists in model Meta options in
django.contrib modules.

The Django "Model Meta options" docs provide examples and generally
point the reader to use lists for the unique_together and ordering
options. Follow our own advice for contrib models.

More generally, lists should be used for homogeneous sequences of
arbitrary lengths of which both unique_together and ordering are.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30947#comment:6>

Django

unread,
Nov 5, 2019, 2:38:28 AM11/5/19
to django-...@googlegroups.com
#30947: Apply data structure best practices to the django.contrib models
-------------------------------------+-------------------------------------
Reporter: Jon Dufresne | Owner: Jon
Type: | Dufresne
Cleanup/optimization | Status: assigned

Component: Documentation | Version: master
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
-------------------------------------+-------------------------------------

Comment (by Jon Dufresne <jon.dufresne@…>):

In [changeset:"e5cacb1f47cb3a2943bbc7685a630c84503b0d1b" e5cacb1]:
{{{
#!CommitTicketReference repository=""
revision="e5cacb1f47cb3a2943bbc7685a630c84503b0d1b"
Refs #30947 -- Changed tuples to lists in model Meta options examples in
docs.

Follow up to 97d3321e89c8d4434927bdbc308db1ccffa99d3b.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30947#comment:5>

Django

unread,
Nov 5, 2019, 2:41:13 AM11/5/19
to django-...@googlegroups.com
#30947: Apply data structure best practices to the django.contrib models
-------------------------------------+-------------------------------------
Reporter: Jon Dufresne | Owner: Jon
Type: | Dufresne
Cleanup/optimization | Status: closed
Component: Documentation | Version: master
Severity: Normal | Resolution: fixed

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
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"2000ed5180308ae844f3df7e4ed1be64e163755b" 2000ed51]:
{{{
#!CommitTicketReference repository=""
revision="2000ed5180308ae844f3df7e4ed1be64e163755b"
[3.0.x] Refs #30947 -- Changed tuples to lists in model Meta options
examples in docs.

Follow up to 97d3321e89c8d4434927bdbc308db1ccffa99d3b.

Backport of e5cacb1f47cb3a2943bbc7685a630c84503b0d1b from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30947#comment:7>

Django

unread,
Nov 5, 2019, 2:41:27 AM11/5/19
to django-...@googlegroups.com
#30947: Apply data structure best practices to the django.contrib models
-------------------------------------+-------------------------------------
Reporter: Jon Dufresne | Owner: Jon
Type: | Dufresne
Cleanup/optimization | Status: closed
Component: Documentation | Version: master
Severity: Normal | Resolution: fixed
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
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"a7f90d9c02f5ed6f6dc1621ef10b9db1f042600b" a7f90d9]:
{{{
#!CommitTicketReference repository=""
revision="a7f90d9c02f5ed6f6dc1621ef10b9db1f042600b"
[2.2.x] Refs #30947 -- Changed tuples to lists in model Meta options
examples in docs.

Follow up to 97d3321e89c8d4434927bdbc308db1ccffa99d3b.

Backport of e5cacb1f47cb3a2943bbc7685a630c84503b0d1b from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30947#comment:8>

Django

unread,
Dec 17, 2020, 6:02:27 AM12/17/20
to django-...@googlegroups.com
#30947: Apply data structure best practices to the django.contrib models and docs

-------------------------------------+-------------------------------------
Reporter: Jon Dufresne | Owner: Jon
Type: | Dufresne
Cleanup/optimization | Status: closed
Component: Documentation | Version: master
Severity: Normal | Resolution: fixed
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
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

#30947 was a duplicate for extra docs changes.

--
Ticket URL: <https://code.djangoproject.com/ticket/30947#comment:9>

Django

unread,
Dec 17, 2020, 6:03:55 AM12/17/20
to django-...@googlegroups.com
#30947: Apply data structure best practices to the django.contrib models and docs
-------------------------------------+-------------------------------------
Reporter: Jon Dufresne | Owner: Jon
Type: | Dufresne
Cleanup/optimization | Status: new

Component: Documentation | Version: master
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 Mariusz Felisiak):

* status: closed => new
* has_patch: 1 => 0
* resolution: fixed =>
* stage: Ready for checkin => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/30947#comment:10>

Django

unread,
Jul 22, 2022, 5:44:48 AM7/22/22
to django-...@googlegroups.com
#30947: Apply data structure best practices to the django.contrib models and docs
--------------------------------------+------------------------------------
Reporter: Jon Dufresne | Owner: (none)
Type: Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev

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 Mariusz Felisiak):

* owner: Jon Dufresne => (none)


* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/30947#comment:11>

Django

unread,
Jul 22, 2022, 5:44:55 AM7/22/22
to django-...@googlegroups.com
#30947: Apply data structure best practices to the django.contrib models and docs
--------------------------------------+------------------------------------
Reporter: Jon Dufresne | Owner: (none)
Type: Cleanup/optimization | Status: new

Component: Documentation | Version: dev
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 Mariusz Felisiak):

* status: assigned => new


--
Ticket URL: <https://code.djangoproject.com/ticket/30947#comment:12>

Django

unread,
Jul 28, 2022, 6:29:30 AM7/28/22
to django-...@googlegroups.com
#30947: Apply data structure best practices to the django.contrib models and docs
--------------------------------------+------------------------------------
Reporter: Jon Dufresne | Owner: (none)
Type: Cleanup/optimization | Status: new

Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0

--------------------------------------+------------------------------------
Changes (by Mariusz Felisiak):

* easy: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/30947#comment:13>

Django

unread,
Jul 28, 2022, 8:54:45 AM7/28/22
to django-...@googlegroups.com
#30947: Apply data structure best practices to the django.contrib models and docs
--------------------------------------+------------------------------------
Reporter: Jon Dufresne | Owner: rihib
Type: Cleanup/optimization | Status: assigned

Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by rihib):

* owner: (none) => rihib


* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/30947#comment:14>

Django

unread,
Jul 30, 2022, 4:22:21 AM7/30/22
to django-...@googlegroups.com
#30947: Apply data structure best practices to the django.contrib models and docs
--------------------------------------+------------------------------------
Reporter: Jon Dufresne | Owner: (none)
Type: Cleanup/optimization | Status: new

Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by rihib):

* owner: rihib => (none)


* status: assigned => new


--
Ticket URL: <https://code.djangoproject.com/ticket/30947#comment:15>

Django

unread,
Jul 30, 2022, 6:18:40 AM7/30/22
to django-...@googlegroups.com
#30947: Apply data structure best practices to the django.contrib models and docs
-------------------------------------+-------------------------------------
Reporter: Jon Dufresne | Owner: Ojas
Type: | Gupta
Cleanup/optimization | Status: assigned

Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Ojas Gupta):

* owner: (none) => Ojas Gupta


* status: new => assigned


Comment:

Ok so I have to convert tuple of list to list of list.

--
Ticket URL: <https://code.djangoproject.com/ticket/30947#comment:16>

Django

unread,
Aug 19, 2022, 2:26:13 PM8/19/22
to django-...@googlegroups.com
#30947: Apply data structure best practices to the django.contrib models and docs
-------------------------------------+-------------------------------------
Reporter: Jon Dufresne | Owner: Ojas
Type: | Gupta
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Alex Morega):

This is actually a pet peeve of mine; would love to see the docs updated
to using lists instead of tuples.

Would this be a good checklist of places to update in the codebase and
docs? https://gist.github.com/mgax/32318118a71ce602a00a80daeecb60fc

--
Ticket URL: <https://code.djangoproject.com/ticket/30947#comment:17>

Django

unread,
Aug 19, 2022, 5:53:25 PM8/19/22
to django-...@googlegroups.com
#30947: Apply data structure best practices to the django.contrib models and docs
-------------------------------------+-------------------------------------
Reporter: Jon Dufresne | Owner: Ojas
Type: | Gupta
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

Some of the examples you pointed should remain tuples as they are mixing
heterogenous data and mostly used to a define a schema over data.

Since we're on the topic of data structure best practices
`unique_together` and friends should likely be defined as `set` and not
`list` as only unique entries will be considered and because the order in
which they are defined on the model doesn't have any significance.

--
Ticket URL: <https://code.djangoproject.com/ticket/30947#comment:18>

Django

unread,
Aug 22, 2022, 3:33:33 AM8/22/22
to django-...@googlegroups.com
#30947: Apply data structure best practices to the django.contrib models and docs
-------------------------------------+-------------------------------------
Reporter: Jon Dufresne | Owner: Ojas
Type: | Gupta
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Alex Morega):

> Some of the examples you pointed should remain tuples as they are mixing
heterogenous data and mostly used to a define a schema over data.

Right! I meant it as a starting point of places that might need fixing.

> Since we're on the topic of data structure best practices
`unique_together` and friends should likely be defined as `set` and not
`list` as only unique entries will be considered and because the order in
which they are defined on the model doesn't have any significance.

I see your point, but AFAIU, a side effect of `unique_together` is
creating composite database indexes, where the
[https://www.postgresql.org/docs/current/indexes-multicolumn.html order of
columns matters].

There hasn't been any activity on the ticket for 3 weeks; may I take
ownership?

--
Ticket URL: <https://code.djangoproject.com/ticket/30947#comment:19>

Django

unread,
Aug 22, 2022, 10:56:53 AM8/22/22
to django-...@googlegroups.com
#30947: Apply data structure best practices to the django.contrib models and docs
-------------------------------------+-------------------------------------
Reporter: Jon Dufresne | Owner: Ojas
Type: | Gupta
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

> I see your point, but AFAIU, a side effect of unique_together is

creating composite database indexes, where the ​order of columns matters.

The order of columns within an index or unique constraints matter, but the
order of constraints doesn't. This obviously means that you can't use the
''flattened'' syntax `index_together = {'foo', 'bar'}` but favor
`index_together = {('foo', 'bar')}`.

--
Ticket URL: <https://code.djangoproject.com/ticket/30947#comment:20>

Django

unread,
Aug 26, 2022, 11:05:16 AM8/26/22
to django-...@googlegroups.com
#30947: Apply data structure best practices to the django.contrib models and docs
-------------------------------------+-------------------------------------
Reporter: Jon Dufresne | Owner: Alex
Type: | Morega

Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Alex Morega):

* owner: Ojas Gupta => Alex Morega


* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/16003 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/30947#comment:21>

Django

unread,
Aug 30, 2022, 3:59:59 AM8/30/22
to django-...@googlegroups.com
#30947: Apply data structure best practices to the django.contrib models and docs
-------------------------------------+-------------------------------------
Reporter: Jon Dufresne | Owner: Alex
Type: | Morega
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/30947#comment:22>

Django

unread,
Aug 30, 2022, 4:29:38 AM8/30/22
to django-...@googlegroups.com
#30947: Apply data structure best practices to the django.contrib models and docs
-------------------------------------+-------------------------------------
Reporter: Jon Dufresne | Owner: Alex
Type: | Morega
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

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

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"de6c9c70549010fc39509f9ef3f6a62ada870318" de6c9c7]:
{{{
#!CommitTicketReference repository=""
revision="de6c9c70549010fc39509f9ef3f6a62ada870318"
Refs #30947 -- Changed tuples to lists where appropriate.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30947#comment:23>

Django

unread,
Aug 30, 2022, 4:29:47 AM8/30/22
to django-...@googlegroups.com
#30947: Apply data structure best practices to the django.contrib models and docs
-------------------------------------+-------------------------------------
Reporter: Jon Dufresne | Owner: Alex
Type: | Morega
Cleanup/optimization | Status: closed
Component: Documentation | Version: dev
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

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


--
Ticket URL: <https://code.djangoproject.com/ticket/30947#comment:24>

Reply all
Reply to author
Forward
0 new messages