#34311 closed Cleanup/optimization (fixed)
Update serialization examples from unique_together to UniqueConstraint
| Reported by: | Willem Van Onsem | Owned by: | Willem Van Onsem |
|---|---|---|---|
| Component: | Documentation | Version: | 4.1 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Ready for checkin | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | yes | UI/UX: | no |
Description (last modified by )
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')]
Change History (10)
comment:1 by , 3 years ago
| Summary: | The documentation on serialization uses unique_together → Update serialization examples from unique_together to UniqueConstraint |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
comment:2 by , 3 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:3 by , 3 years ago
| Has patch: | set |
|---|---|
| Owner: | changed from to |
follow-up: 5 comment:4 by , 3 years ago
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 ]
comment:5 by , 3 years ago
Replying to 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
UniqueConstraintwith 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.
comment:6 by , 3 years ago
| Description: | modified (diff) |
|---|
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:7 by , 3 years ago
| Patch needs improvement: | set |
|---|
comment:8 by , 3 years ago
| Patch needs improvement: | unset |
|---|---|
| Triage Stage: | Accepted → Ready for checkin |
PR