我的代码有什么问题?
class Group(ImageModel):
title = models.CharField(verbose_name = "Title", max_length=7)
photos = models.ManyToManyField('Photo', related_name='+',
verbose_name=_('Photo'),
null=True, blank=True)
.....
pid = Photo.objects.get(image = str_path)
gid= Group.objects.get(id = self.id)
self.save_photos(gid, pid)
....
def save_photos(self, gid, pid):
group_photo = GroupPhotos(groupupload=gid.id,
photo=pid.id
)
group_photo.save()
我的 GroupPhotos 模型是:
class GroupPhotos(models.Model):
groupupload = models.ForeignKey('Group')
photo = models.ForeignKey('Photo')
class Meta:
db_table = u'group_photos'
当我想从管理面板中保存它时,我收到这样的值错误:
Cannot assign "38": "GroupPhotos.groupupload" must be a "Group" instance.
使用group_photo = GroupPhotos(groupupload=gid, photo=pid
) 定义它正在工作,但 GroupPhotos 表 (group_photos) 中没有任何更改。打印这个print pid.id,' >>> ',gid.id
我得到了真正的关系......
更新:
我从早上开始一直在工作,但没有任何进展……我也尝试过,但没有任何改变:
pid = Photo.objects.get(image = str_path)
ger = Group.objects.get(id = self.id)
ger.title = self.title
ger.save()
ger.photos.add(pid)