Django使用外键的post_save信号

时间:2020-03-05 08:00:17

标签: django django-views

我还不太精通Django信号。

我只希望在 User 中每输入6个新条目,保存在 User_GraduateList 中的第一个配置文件将自动保存在 GraduateUser < / p>

用户

enter image description here

User_GraduateList

enter image description here

这是我想要表 GraduateUser

中的结果

enter image description here 因为Joe Dio是 User_GraduateList

中的第一个条目

这是我的模特

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)

    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)

@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 GraduateUser(models.Model):
    User = models.ForeignKey(User_GraduateList, on_delete=models.CASCADE, blank=True)

    def __str__(self):
        suser = '{0.User}'
        return suser.format(self)

@receiver(post_save, sender=User)
def create_graduates(sender, instance, created, **kwargs):
    sponsor = instance
    if created and sponsor:
        print("1")
        if sponsor.user_set.count() >= 6:
            print("2")
            if not GraduateUser.objects.filter(User=sponsor).exists():
                print("3")
                GraduateUser.objects.create(User=sponsor)

我没有收到任何错误

注意:该问题与我之前的问题Django Signal post_save()

不太相似

更新

@receiver(post_save, sender=User)
    def create_graduates(sender, instance, created, **kwargs):
        sponsor = instance
        print(instance)
        if created and sponsor:
            print("1")
            if sponsor.user_set.count() >= 6:
                print("2")
                if not GraduateUser.objects.filter(User=sponsor).exists():
                    print("3")
                    GraduateUser.objects.create(User=sponsor)

当我尝试在表用户中插入数据时,

我在cmd或终端中收到每个新的Entery的ID和1

 >>new entry
 >>1

请读这个家伙,

如果用户收到6个新条目,则第一个配置文件保存在 User_GraduateList 中,然后删除并保存到另一个表 GraduateUser

Django Signal post_save()和此问题之间的区别是,一旦系统检测到 User.Sponsor_User (模型 User 中的外键)已邀请2个用户,则用户将自动保存在 User_GraduateList

再次更新

@receiver(post_save, sender=User)
def im_graduate(sender, instance, created, **kwargs):
    s = User_GraduateList.objects.filter(id = instance.id).order_by('id')
    graduate = instance
    if created and graduate:
        print(graduate.user_set.count())
        if graduate.user_set.count() == 6:
            if not GraduateUser.objects.filter(User=s).exists():
                GraduateUser.objects.create(User=s)

我尝试打印(graduate.user_set.count())结果始终为>> 0,它不计入用户表

中插入的新条目

另一个例子

对于用户表

中插入的每个新条目
>>New Entry 1
>>New Entry 2
>>New Entry 3
>>New Entry 4
>>New Entry 5
>>New Entry 6

如果到达新条目6 ,则第一个保存在 User_GraduateList表中的条目将删除并保存到另一个表GraduateUser

1 个答案:

答案 0 :(得分:0)

根据我的understood,我将尝试回答:

首先,我认为在GraduateUser中,外键关系应该在User上,而不是User_GraduateList

class GraduateUser(models.Model):
    User = models.ForeignKey(User, 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:
        user_count = sponsor.user_set.count()
        if 2 <= user_count < 6:
            if not User_GraduateList.objects.filter(User=sponsor).exists():
                User_GraduateList.objects.create(User=sponsor)
        elif user_count >= 6:
            User_GraduateList.objects.filter(User=sponsor).delete()
            if not GraduateUser.objects.filter(User=sponsor).exists():
                GraduateUser.objects.create(User=sponsor)