只有在成功创建其他对象时才创建对象
Create object only if other object is successfully created
我是 Django 的新手,不熟悉这种情况的最佳实践(在任何 framework/language,而不仅仅是 python/django 中)。
情况是,当用户第一次在我的网站上注册时,我想为他们创建一个 "organization"(如果它不存在),然后为他们创建一个引用组织的用户.我永远不想在没有另一个的情况下插入一个,但我需要先创建组织,以便可以为每个用户保存组织 UUID。现在,即使用户的创建出现问题,组织仍将被创建。这显然是一个问题,因为那时我有一个没有用户的组织。
我不完全知道如何在创建组织之前检查用户是否已正确创建,但我似乎需要按照这些思路做一些事情。在创建组织对象时使用 commit=false 似乎不起作用,因为我需要获取 UUID。所以我不确定继续进行的最佳方式。
我正在覆盖流行的身份验证包 django-allauth 的序列化程序中的保存方法
models.py
class Organization(models.Model):
alphanumeric_plus_underscore = RegexValidator(r'^[\w]+$', 'Only alphanumeric characters are allowed.')
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) # pylint: disable=invalid-name
name = models.CharField(max_length=20, unique=True, validators=[alphanumeric_plus_underscore, MinLengthValidator(4)])
logo = models.FileField(upload_to='files/organization_logos/', null=True, blank=True)
class User(AbstractBaseUser):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) # pylint: disable=invalid-name
first_name = models.CharField(_('First Name'), max_length=50)
last_name = models.CharField(_('Last Name'), max_length=50)
email = models.EmailField(_('Email address'), unique=True)
organization = models.ForeignKey(Organization, blank=False, null=False, on_delete=models.DO_NOTHING)
serializers.py
def save(self, request):
# generate organization object
organization_data = self.validated_data.pop('organization')
organization = Organization.objects.create(**organization_data)
self.validated_data['organization'] = organization
adapter = get_adapter()
user = adapter.new_user(request)
self.cleaned_data = self.get_cleaned_data()
user.organization = organization #self.cleaned_data.get('organization')
adapter.save_user(request, user, self)
self.custom_signup(request, user)
setup_user_email(request, user, [])
return user
非常感谢任何指导。
原子性是数据库事务的定义 属性。 atomic 允许我们创建一个代码块,其中保证了数据库的原子性。如果代码块成功完成,更改将提交到数据库。如果出现异常,则回滚更改。
from django.db import transaction
def viewfunc(request):
# This code executes in autocommit mode (Django's default).
do_stuff()
with transaction.atomic():
# This code executes inside a transaction.
do_more_stuff()
有关给定 link 的更多详细信息 refer。
I want to create an "organization" for them if it doesn't exists
使用Queryset.get_or_create
获取或创建组织。
将所有这些包装在 transaction 中。
from django.db import transaction
with transaction.atomic():
organization, created = Organization.objects.get_or_create(**organization_data)
# Try creating user and if that fails, raise an Exception.
# This way organisation created in the transaction is rolled back.
您必须将信号添加到您的用户模型中。像这样:
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_organization(sender, instance=None, created=False, **kwargs):
if created:
organization.objects.create(user=instance)
这对我有用。当我需要在用户创建时创建一些对象。它在创建用户实例后自动调用。
我是 Django 的新手,不熟悉这种情况的最佳实践(在任何 framework/language,而不仅仅是 python/django 中)。
情况是,当用户第一次在我的网站上注册时,我想为他们创建一个 "organization"(如果它不存在),然后为他们创建一个引用组织的用户.我永远不想在没有另一个的情况下插入一个,但我需要先创建组织,以便可以为每个用户保存组织 UUID。现在,即使用户的创建出现问题,组织仍将被创建。这显然是一个问题,因为那时我有一个没有用户的组织。
我不完全知道如何在创建组织之前检查用户是否已正确创建,但我似乎需要按照这些思路做一些事情。在创建组织对象时使用 commit=false 似乎不起作用,因为我需要获取 UUID。所以我不确定继续进行的最佳方式。
我正在覆盖流行的身份验证包 django-allauth 的序列化程序中的保存方法
models.py
class Organization(models.Model):
alphanumeric_plus_underscore = RegexValidator(r'^[\w]+$', 'Only alphanumeric characters are allowed.')
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) # pylint: disable=invalid-name
name = models.CharField(max_length=20, unique=True, validators=[alphanumeric_plus_underscore, MinLengthValidator(4)])
logo = models.FileField(upload_to='files/organization_logos/', null=True, blank=True)
class User(AbstractBaseUser):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) # pylint: disable=invalid-name
first_name = models.CharField(_('First Name'), max_length=50)
last_name = models.CharField(_('Last Name'), max_length=50)
email = models.EmailField(_('Email address'), unique=True)
organization = models.ForeignKey(Organization, blank=False, null=False, on_delete=models.DO_NOTHING)
serializers.py
def save(self, request):
# generate organization object
organization_data = self.validated_data.pop('organization')
organization = Organization.objects.create(**organization_data)
self.validated_data['organization'] = organization
adapter = get_adapter()
user = adapter.new_user(request)
self.cleaned_data = self.get_cleaned_data()
user.organization = organization #self.cleaned_data.get('organization')
adapter.save_user(request, user, self)
self.custom_signup(request, user)
setup_user_email(request, user, [])
return user
非常感谢任何指导。
原子性是数据库事务的定义 属性。 atomic 允许我们创建一个代码块,其中保证了数据库的原子性。如果代码块成功完成,更改将提交到数据库。如果出现异常,则回滚更改。
from django.db import transaction
def viewfunc(request):
# This code executes in autocommit mode (Django's default).
do_stuff()
with transaction.atomic():
# This code executes inside a transaction.
do_more_stuff()
有关给定 link 的更多详细信息 refer。
I want to create an "organization" for them if it doesn't exists
使用Queryset.get_or_create
获取或创建组织。
将所有这些包装在 transaction 中。
from django.db import transaction
with transaction.atomic():
organization, created = Organization.objects.get_or_create(**organization_data)
# Try creating user and if that fails, raise an Exception.
# This way organisation created in the transaction is rolled back.
您必须将信号添加到您的用户模型中。像这样:
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_organization(sender, instance=None, created=False, **kwargs):
if created:
organization.objects.create(user=instance)
这对我有用。当我需要在用户创建时创建一些对象。它在创建用户实例后自动调用。