1
post_save 트리거에서 ManyToMany 필드로 작업하려고합니다. 모범ManyToMany 필드가 pre_ (post_) 저장 트리거에 할당되지 않았습니다.
@receiver(post_save, sender=Post, dispatch_uid='update_post_images')
def update_post_images(sender, instance, using, **kwargs):
post_save.disconnect(update_post_images, sender=Post, dispatch_uid='update_post_images')
print 'before', instance.images.all()
img = Image.object.get(pk=1469)
instance.images.add(img)
print 'after', instance.images.all()
post_save.connect(update_post_images, sender=Post, dispatch_uid='update_post_images')
지금 내가 정확히 내가 원하는 것을 볼 콘솔을 장고을 볼 때. print 'before'
은 하나의 이미지 객체를 출력하고 print 'after'
- 2 객체는 입니다. 그러나 파이썬 콘솔 (manage.py 쉘)에서 동일한 포스트 객체를 쿼리 할 때 하나의 이미지 만 있습니다.
사람이
나는 그것이 내가 필요한 것이 아니라고 생각한다. 한번 더 post_save 트리거에서 ManytoMany 필드에 일부 값을 채워야합니다. 예를 들어 사용자가 모델 포스트에서 텍스트 필드를 변경했습니다. post_save에서 img 태그에 대한 텍스트를 파싱하고 Post.images를 채 웁니다 - 그 값이있는 필드 – 1099511627776
몇 가지 조사를 한 후에 다음을 발견했습니다. http://stackoverflow.com/questions/4432385/django-how-to-save- m2m-data-via-post-save-signal 링크와 잘 작동했습니다. 그래서 네가 옳았 어. – 1099511627776