1
0
mirror of https://github.com/django/django.git synced 2025-06-05 03:29:12 +00:00

[4.2.x] Fixed #34311 -- Updated serialization docs from unique_together to UniqueConstraint.

Backport of 292aacaf6c3d6956ca2c51c41e36dbf425389346 from main
This commit is contained in:
Willem Van Onsem 2023-02-04 22:48:44 +01:00 committed by Mariusz Felisiak
parent efe16fd72a
commit 7a88b1f5aa

View File

@ -387,7 +387,12 @@ Consider the following two models::
birthdate = models.DateField() birthdate = models.DateField()
class Meta: class Meta:
unique_together = [['first_name', 'last_name']] constraints = [
models.UniqueConstraint(
fields=["first_name", "last_name"],
name="unique_first_last_name",
),
]
class Book(models.Model): class Book(models.Model):
name = models.CharField(max_length=100) name = models.CharField(max_length=100)
@ -431,7 +436,12 @@ name::
objects = PersonManager() objects = PersonManager()
class Meta: class Meta:
unique_together = [['first_name', 'last_name']] constraints = [
models.UniqueConstraint(
fields=["first_name", "last_name"],
name="unique_first_last_name",
),
]
Now books can use that natural key to refer to ``Person`` objects:: Now books can use that natural key to refer to ``Person`` objects::
@ -454,12 +464,12 @@ into the primary key of an actual ``Person`` object.
Whatever fields you use for a natural key must be able to uniquely Whatever fields you use for a natural key must be able to uniquely
identify an object. This will usually mean that your model will identify an object. This will usually mean that your model will
have a uniqueness clause (either unique=True on a single field, or have a uniqueness clause (either ``unique=True`` on a single field, or a
``unique_together`` over multiple fields) for the field or fields ``UniqueConstraint`` or ``unique_together`` over multiple fields) for the
in your natural key. However, uniqueness doesn't need to be field or fields in your natural key. However, uniqueness doesn't need to be
enforced at the database level. If you are certain that a set of enforced at the database level. If you are certain that a set of fields
fields will be effectively unique, you can still use those fields will be effectively unique, you can still use those fields as a natural
as a natural key. key.
Deserialization of objects with no primary key will always check whether the Deserialization of objects with no primary key will always check whether the
model's manager has a ``get_by_natural_key()`` method and if so, use it to model's manager has a ``get_by_natural_key()`` method and if so, use it to
@ -479,7 +489,12 @@ Firstly, you need to add another method -- this time to the model itself::
objects = PersonManager() objects = PersonManager()
class Meta: class Meta:
unique_together = [['first_name', 'last_name']] constraints = [
models.UniqueConstraint(
fields=["first_name", "last_name"],
name="unique_first_last_name",
),
]
def natural_key(self): def natural_key(self):
return (self.first_name, self.last_name) return (self.first_name, self.last_name)