Django 信号:不能使用 For 循环?

Django Signals: Can't Use For Loop?

当我删除信号中的 for 循环时,它就可以工作(正确创建一个对象)但是当我使用 for 循环时,它应该为 Collection 对象中的每个 post 创建一个对象,但这不是'不工作。它甚至不创建 Collection_List_Item 模型的对象。这个 for 循环不起作用的原因是什么?有没有办法解决这个问题?

型号

class Collection(models.Model):
    posts = models.ManyToManyField(Post, related_name='collection_posts', blank=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    collection_name = models.CharField(max_length=100)
    collection_description = models.CharField(max_length=1000, blank=True)
    collection_likes = models.ManyToManyField(User, related_name='liked_collections', blank=True)
    collection_image = models.ImageField(upload_to="images/")
    private = models.BooleanField(default=False)
    follows = models.ManyToManyField(User, related_name='collection_follows', blank=True)

    def __str__(self):
        return self.collection_name

class Collection_List_Item(models.Model):
    post = models.ForeignKey(Post, on_delete=models.CASCADE, null=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    collection = models.ForeignKey(Collection, on_delete=models.CASCADE, null=True)
    saved = models.BooleanField(default=False)
    created_date = models.DateTimeField(auto_now_add=True)
    modified_date = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.collection.collection_name

信号:

@receiver(post_save, sender=Collection)
def create_collection_list_item(sender, instance, created, **kwargs):
    if created:
        for i in instance.posts.all():
            collection_list_item = Collection_List_Item.objects.create(collection=instance, user=instance.author, post=i)
        
            collection_list_item.save()

对于 ManyToManyField 字段,您必须使用 m2m_changed (Django Docs) 信号。

因为ManyToManyField是在实例保存后保存的,所以ManyToManyField次更新根本不会有任何记录。

from django.db.models.signals import m2m_changed
from django.db import IntegrityError

@receiver(m2m_changed, sender=Collection.posts.through)
def create_collection_list_item(sender, instance, action, *args, **kwargs):
    if action == "post_add":
        for i in instance.posts.all():
            try:
                collection_list_item = Collection_List_Item.objects.create(collection=instance, user=instance.author, post=i)
            except IntegrityError:
                pass