Django信号post_save() [英] Django Signal post_save()

查看:58
本文介绍了Django信号post_save()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

系统检测到您已经邀请2位用户后,您的个人资料将自动保存在毕业生列表中

Once the system detects that you already invited 2 users, your profile will automatically save in graduate list

请查看此图片,例如:

这是毕业生列表的表.

如图所示, Joe Dio 已经邀请了2位用户( miro justin ),(您可以看到在赞助者用户)

as you can see in the picture, Joe Dio has already invited 2 users (miro and justin), (you can see it in the Sponsor user)

我希望您能帮助我使用django信号来解决它,在此先感谢:)

I hope you can help me to figure it out using django signal, thanks in advance :)

我希望我做的例子能使您理解我想要得到的东西

i wish the example I made had you understood what i wanted to get

这是我的模特.py

class User(models.Model):
    firstname = models.CharField(max_length=500, null=True,blank=True)
    lastname = models.CharField(max_length=500, null=True, blank=True)
    middlename = models.CharField(max_length=500, null=True, blank=True)

    birthday = models.CharField(max_length=500, null=True, blank=True)
    Email  =  models.CharField(max_length=500,null=True,blank=True)
    Sponsor_User = models.ForeignKey('self', on_delete=models.CASCADE,blank=True, null=True)

class User_GraduateList(models.Model):
    User = models.ForeignKey(User, related_name='+', on_delete=models.CASCADE, blank=True)
    def __str__(self):
        suser = '{0.User}'

更新,这是@AKS先生的答案

UPDATE this is the answer of mr @AKS

class User_GraduateList(models.Model):
    User = models.ForeignKey(User, related_name='+', on_delete=models.CASCADE, blank=True)

    @receiver(post_save, sender=User)
    def create_graduates(sender, instance, created, **kwargs):
        sponsor = instance.Sponsor_User
        if created and sponsor:
            if sponsor.user_set.count() >= 2:
                if not User_GraduateList.objects.filter(User=sponsor).exists():
                    User_GraduateList.objects.create(User=sponsor)

它没有用.

推荐答案

class User_GraduateList(models.Model):
    User = models.ForeignKey(User, related_name='+', on_delete=models.CASCADE, blank=True)

@receiver(post_save, sender=User)
def create_graduates(sender, instance, created, **kwargs):
    sponsor = instance.Sponsor_User
    if created and sponsor:
        if sponsor.user_set.count() >= 2:
            if not User_GraduateList.objects.filter(User=sponsor).exists():
                User_GraduateList.objects.create(User=sponsor)

出于阅读目的,我嵌套了if条件.如果您感到舒适,则可以使用加入.

For reading purposes, I have nested the if conditions. If you feel comfortable, you can join then using and.

以上解决方案基于以下假设:我们仅在创建新用户时才想这样做.如果以后可以设置发起人用户,则可以从第一个条件中删除创建的检查.

The solution above is based on the assumption that we only want to do this when a new user is created. If the sponsor user can be set later, the created check can be removed from the first condition.

另外,查看模型,我认为最好在 User_GraduateList User 模型之间建立一对一的关系.

Also, looking at the models, I think it will be best to have a one to one relationship between the User_GraduateList and User model.

这篇关于Django信号post_save()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆