我想知道有什么方法可以使用信号获取一个模型中更新后的多对多字段并将其发布到另一模型中
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.
答案 0 :(得分: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'],
)
在这里,实例是Post
的实例,而不是User
,您也在创建者字段中也使用了post
实例< / strong>。这是导致错误的原因。
要解决此错误,您需要在这些字段中传递用户实例。例如,您可以使用类似的内容:
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()