调用父类save获得一个对象,为什么这个对象是Weibo,不是WeiboImage?
来源:4-2 文件上传之ORM上传
老中医学编程
2019-07-30 09:58:35
class UploadModelForm(forms.ModelForm): content = forms.CharField(label='请输入微博内容',max_length=300,widget=forms.Textarea) class Meta: model=WeiboImage fields=['image'] def save(self,user,content,commit=False): obj = super().save(commit) #1.创建weibo对象 weibo = Weibo.objects.create(user=user,content=content) obj.weibo = weibo obj.save()
怎么确定返回的这个对象是哪个?
1回答
时间,
2019-07-30
同学,你好。调用父类得到的obj对象为WeiboImage object,同学可以将obj进行打印。WeiboImage与Weibo表存在外键关联,因此可以通过obj.weibo得到weibo对象并重新赋值。
如果我的回答解决了您的疑惑,请采纳!祝学习愉快~~~~
相似问题