【问题标题】:Using signals for two models both inheriting from User in Django在 Django 中为两个都继承自 User 的模型使用信号
【发布时间】:2021-12-29 11:57:49
【问题描述】:

假设我们有两个模型向用户模型发送信号:

from django.db import models
from django.contrib.auth.models import User
from django.db.models import signals

class Company(User):
    name = models.CharField(null=True, blank=True, max_length=30)
            if created:
            Company.objects.create(
                user_ptr_id=instance.id,
                username=instance.username,
                password=instance.password,
                email=instance.email,
                first_name=instance.first_name,
                last_name=instance.last_name,
                is_active=instance.is_active,
                is_superuser=instance.is_superuser,
                is_staff=instance.is_staff,
                date_joined=instance.date_joined,
            )

    signals.post_save.connect(
        create_company, sender=User, weak=False, dispatch_uid="create_companies"
    )


class Individual(User):
    name = models.CharField(null=True, blank=True, max_length=30)

    def create_job_seeker(sender, instance, created, **kwargs):
        """
        :param instance: Current context User instance
        :param created: Boolean value for User creation
        :param kwargs: Any
        :return: New Seeker instance
        """
        if created: 
            '''
            we should add a condition on whether the Company uses the same username
            if true, then, we must not create a JobSeeker and we would disable the account using
            Firebase Admin
            '''
            JobSeeker.objects.create(
                user_ptr_id=instance.id,
                username=instance.username,
                password=instance.password,
                email=instance.email,
                first_name=instance.first_name,
                last_name=instance.last_name,
                is_active=instance.is_active,
                is_superuser=instance.is_superuser,
                is_staff=instance.is_staff,
                date_joined=instance.date_joined,
            )

    signals.post_save.connect(
        create_job_seeker, sender=User, weak=False, dispatch_uid="create_job_seekers"
    )

现在,每次创建用户时,我们都应该被允许通过个人和公司模型来扩展它。但是,我想禁止使用这两个对象。用户可以拥有要编辑的公司或个人对象,不能同时编辑。我应该覆盖这样的保存方法吗:

    def save(self, *args, **kwargs):
        if not Company.objects.filter(username=self.username).exists():
             super(Model, self).save(*args, **kwargs)
        else:
            raise 'Some error'

或者我应该在创建的方法上添加一个条件,例如:

...
    if created and Company.objects.filter(username, self.username).exists() == False:
         Company.objects.create( 
...

哪种方法更好?您可能会建议其他方法吗?

【问题讨论】:

    标签: django database django-models django-rest-framework django-forms


    【解决方案1】:

    在大多数情况下,我认为信号是处理模型之间共享数据的最佳方式,假设每个相关模型的 CRUD 没有一起完成。因此 post_save、pre_save、post_delete、pre_delete 等通常是处理任何给定模型实例所依赖的数据的最佳方式。这对于在保存后操作模型数据是正确的。信号是专门为此设计的。关于信号的另一个好处是您可以在整个项目中连接它们,而不必只是定义模型的地方。只需导入模型和要连接的信号即可!

    如何使用信号?在此处关注 django 的文档。很简单

    https://docs.djangoproject.com/en/4.0/topics/signals/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-09
      • 2021-06-18
      • 1970-01-01
      • 2021-08-20
      • 1970-01-01
      • 2017-12-18
      • 2017-09-18
      • 1970-01-01
      相关资源
      最近更新 更多