如何使用Django信号从Django的manytomanyfield中获取用户,并使用接收到的manytomany用户字段创建另一个对象

问题描述 投票:0回答:1

我想知道有什么方法可以使用信号在一个模型中获取更新的多对多字段并将其发布到另一模型中

class Post(models.Model):

    name=models.CharField(max_length=24)
    nc=models.ManyToManyField(settings.AUTH_USER_MODEL)
    def __str__(self):
        return self.name
        # return self.name
m2m_changed.connect(receiver=like_post,sender=Post.nc.through)

我想在后模型的nc字段中收集更新的记录,并使用要使用功能创建对象的信号这是连接到Post模型的信号

def like_post(sender, *args, **kwargs):
    # if kwargs['action'] in ('post_update'):
    if kwargs['action'] in ('post_add', 'post_remove','post_update'):

        print(kwargs['instance'])
        instance = kwargs['instance']
        print(instance)
        notify = Notify.objects.create(
                 recipient=instance,
                 creator=Post.objects.get(pk=50),
                  state='unread',
                   type=kwargs['action'],
                )
    else:
        print('no instance') 

在接收者和创建者部分中,我想使用现有的用户对象来更新这些字段,创建者是更新manytomanyfield的人,接收者是创建该帖子的人

notify model:
class Notify(models.Model):
    recipient = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='notify_recipient',on_delete=models.CASCADE)
    creator = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='notify_sender',on_delete=models.CASCADE)
    state = ('read', 'unread', 'unseen')
    type = models.CharField(max_length=30, blank=True)
    url = models.CharField(max_length=50, blank=True)

每当我运行此实例时,该实例只会打印帖子对象名称并触发此错误

ValueError at /admin/posts/post/50/change/
Cannot assign "<Post: ok>": "Notify.recipient" must be a "User" instance.
python django django-models django-views django-signals
1个回答
1
投票

您可以看到您的Notify类将receipent定义为AUTH_USER_MODEL的ForeignKey元素,但是您正在信号中创建Notify如下:

notify = Notify.objects.create(
                 recipient=instance,
                 creator=Post.objects.get(pk=50),
                  state='unread',
                   type=kwargs['action'],
                )

这里,instancePost的实例,而不是User,也您也在创建者字段中也使用post实例]。这是导致错误的原因。

要解决此错误,您需要在这些字段中传递用户实例。例如,您可以使用类似:

notify = Notify.objects.create(
                     recipient=instance.nc, # find out how to pass the user of the post here
                     creator=Post.objects.get(pk=50), # replace by an user instance here instead of a post instance 
                      state='unread',
                       type=kwargs['action'],
                    )

编辑:要确保已保存用户实例,您需要使用以下模型覆盖ModelAdmin的save_model方法:

class PostAdmin(admin.ModelAdmin):
    def save_related(self, request, form, formsets, change):
    if not form.cleaned_data['nc']:
        form.cleaned_data['nc'] = [request.user]
    form.save_m2m()
    
© www.soinside.com 2019 - 2024. All rights reserved.