自2013年起 或者,获得原产地为原子,因此,它处理的是共同点:
This method is atomic assuming correct usage, correct database
configuration, and correct behavior of the underlying database.
However, if uniqueness is not enforced at the database level for the
kwargs used in a get_or_create call (see unique or unique_together),
this method is prone to a race-condition which can result in multiple
rows with the same parameters being inserted simultaneously.
If you are using MySQL, be sure to use the READ COMMITTED isolation
level rather than REPEATABLE READ (the default), otherwise you may see
cases where get_or_create will raise an IntegrityError but the object
won’t appear in a subsequent get() call.
http://docs.djangoproject.com/en/dev/ref/models/querysets/#get-or-create”rel=“noreferer”
Here s an example of how you could do it:
1. 界定具有独特性的模式 真:
class MyModel(models.Model):
slug = models.SlugField(max_length=255, unique=True)
name = models.CharField(max_length=255)
MyModel.objects.get_or_create(slug=<user_slug_here>, defaults={"name": <user_name_here>})
......或通过使用独一无二的“togheter”:
class MyModel(models.Model):
prefix = models.CharField(max_length=3)
slug = models.SlugField(max_length=255)
name = models.CharField(max_length=255)
class Meta:
unique_together = ("prefix", "slug")
MyModel.objects.get_or_create(prefix=<user_prefix_here>, slug=<user_slug_here>, defaults={"name": <user_name_here>})
说明非岛屿田地是如何处于缺省状态的,NOT是独一无二的田地。 这将确保你的创造是原子的。
这里指的是它如何在Django执行:rel=“noreferer”https://github.com/django/django/django/blob/fd60e688c986a102f0125d9dcdcf46pf61fmodgf - 制造物体,捕获最终的正直,并归还该笔物品。 换言之,在数据库中处理原子问题。