Re: [Django] #34311: Update serialization examples from unique_together to UniqueConstraint

1 view
Skip to first unread message

Django

unread,
Feb 5, 2023, 9:49:52 AM2/5/23
to django-...@googlegroups.com
#34311: Update serialization examples from unique_together to UniqueConstraint
-------------------------------------+-------------------------------------
Reporter: Willem Van Onsem | Owner: Moez
Type: | Saidi
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 4.1
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 Moez Saidi):

* owner: nobody => Moez Saidi
* status: new => assigned


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

Django

unread,
Feb 5, 2023, 12:58:30 PM2/5/23
to django-...@googlegroups.com
#34311: Update serialization examples from unique_together to UniqueConstraint
-------------------------------------+-------------------------------------
Reporter: Willem Van Onsem | Owner: Willem
Type: | Van Onsem

Cleanup/optimization | Status: assigned
Component: Documentation | Version: 4.1
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 Mariusz Felisiak):

* owner: Moez Saidi => Willem Van Onsem
* has_patch: 0 => 1


Comment:

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

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

Django

unread,
Feb 5, 2023, 2:13:45 PM2/5/23
to django-...@googlegroups.com
#34311: Update serialization examples from unique_together to UniqueConstraint
-------------------------------------+-------------------------------------
Reporter: Willem Van Onsem | Owner: Willem
Type: | Van Onsem
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 4.1
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
-------------------------------------+-------------------------------------

Comment (by Waqar ALi):

I believe you're confusing model meta option `constratins` with
`unique_together`. `UniqueConstraint` is a DB constraint assigned to
constraints option in Meta whereas unique together is another option on
Meta level.


{{{
class Mymodel(model):
class Meta:
unique_together = [ 'field1', 'field2' ]
constratints = [ UniqueConstraint ]
}}}

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

Django

unread,
Feb 5, 2023, 2:22:55 PM2/5/23
to django-...@googlegroups.com
#34311: Update serialization examples from unique_together to UniqueConstraint
-------------------------------------+-------------------------------------
Reporter: Willem Van Onsem | Owner: Willem
Type: | Van Onsem
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 4.1
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
-------------------------------------+-------------------------------------

Comment (by Willem Van Onsem):

Replying to [comment:4 Waqar ALi]:
No, Django had in the early days `unique_together`, now it still has, but
as the note on the documentation says
(https://docs.djangoproject.com/en/4.1/ref/models/options/#django.db.models.Options.unique_together):

> Use `UniqueConstraint` with the constraints option instead.

It "translated" to a unique constraint. But it was less flexible since one
could not give it a custom name, add a condition, make it a functional
unique constraint, etc. So `UniqueConstraint` is the "new way" to define
this, whereas `unique_together` is the "old" way to generate such database
constraint.

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

Django

unread,
Feb 5, 2023, 2:28:26 PM2/5/23
to django-...@googlegroups.com
#34311: Update serialization examples from unique_together to UniqueConstraint
-------------------------------------+-------------------------------------
Reporter: Willem Van Onsem | Owner: Willem
Type: | Van Onsem
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 4.1
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
-------------------------------------+-------------------------------------

Old description:

> Documentation on serialization uses `unique_together`, whereas it seems
> to be advised to use Django's constraint framework. Therefore this might
> introduce confusion. Perhaps we should "update" the documentation.
>
> https://docs.djangoproject.com/en/dev/topics/serialization/

New description:

The idea is thus to transform code on the page like:

{{{
class Mymodel(model):
class Meta:
unique_together = [[ 'field1', 'field2' ]]
}}}

to:

{{{
class Mymodel(model):
class Meta:
constraints = [models.UniqueConstraint(fields=('field1',
'field2'), name='field1_2_unique')]
}}}

--

Comment (by Willem Van Onsem):

The idea is thus to transform code on the page like:

{{{
class Mymodel(model):
class Meta:
unique_together = [[ 'field1', 'field2' ]]
}}}

to:

{{{
class Mymodel(model):
class Meta:
constraints = [models.UniqueConstraint(fields=('field1',
'field2'), name='field1_2_unique')]
}}}

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

Django

unread,
Feb 5, 2023, 11:48:36 PM2/5/23
to django-...@googlegroups.com
#34311: Update serialization examples from unique_together to UniqueConstraint
-------------------------------------+-------------------------------------
Reporter: Willem Van Onsem | Owner: Willem
Type: | Van Onsem
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 4.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 0 => 1


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

Django

unread,
Feb 8, 2023, 11:31:47 PM2/8/23
to django-...@googlegroups.com
#34311: Update serialization examples from unique_together to UniqueConstraint
-------------------------------------+-------------------------------------
Reporter: Willem Van Onsem | Owner: Willem
Type: | Van Onsem
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 4.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: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin


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

Django

unread,
Feb 8, 2023, 11:56:23 PM2/8/23
to django-...@googlegroups.com
#34311: Update serialization examples from unique_together to UniqueConstraint
-------------------------------------+-------------------------------------
Reporter: Willem Van Onsem | Owner: Willem
Type: | Van Onsem
Cleanup/optimization | Status: closed
Component: Documentation | Version: 4.1
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 <felisiak.mariusz@…>):

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


Comment:

In [changeset:"292aacaf6c3d6956ca2c51c41e36dbf425389346" 292aaca]:
{{{
#!CommitTicketReference repository=""
revision="292aacaf6c3d6956ca2c51c41e36dbf425389346"
Fixed #34311 -- Updated serialization docs from unique_together to
UniqueConstraint.
}}}

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

Django

unread,
Feb 8, 2023, 11:56:54 PM2/8/23
to django-...@googlegroups.com
#34311: Update serialization examples from unique_together to UniqueConstraint
-------------------------------------+-------------------------------------
Reporter: Willem Van Onsem | Owner: Willem
Type: | Van Onsem
Cleanup/optimization | Status: closed
Component: Documentation | Version: 4.1
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
-------------------------------------+-------------------------------------

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

In [changeset:"7a88b1f5aaea0c52e16aabbdd7a8ea57ca0f6aea" 7a88b1f]:
{{{
#!CommitTicketReference repository=""
revision="7a88b1f5aaea0c52e16aabbdd7a8ea57ca0f6aea"
[4.2.x] Fixed #34311 -- Updated serialization docs from unique_together to
UniqueConstraint.

Backport of 292aacaf6c3d6956ca2c51c41e36dbf425389346 from main
}}}

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

Reply all
Reply to author
Forward
0 new messages