【发布时间】:2012-07-12 20:23:04
【问题描述】:
我有一个 post 模型和一个 postImage 模型:
class PostImage(models.Model):
post = models.ForeignKey(Post, related_name="images")
# @@@@ figure out a way to have image folders per user...
image = models.ImageField(upload_to='images')
description = models.CharField(max_length=100)
order = models.IntegerField()
我已经为两者创建了美味的资源:
class PostImageResource(ModelResource):
class Meta:
queryset = PostImage.objects.all()
resource_name = 'postImage'
class PostResource(ModelResource):
images = fields.ForeignKey(PostImageResource, 'images', full=True)
class Meta:
queryset = Post.objects.all()
resource_name = 'post'
当我尝试浏览到 api/v1/post/?format=json 时,我收到以下错误:
The object '' has an empty attribute 'image' and doesn't allow a default or null value.
我还没有任何 PostImage。
如果我去掉PostResource 中的images = fields.foreignKey 行,它就可以工作。如果我浏览到api/v1/postImage/?format=json,则不会显示任何错误,只是一个空集合。
【问题讨论】:
-
您是否检查过您的所有 PostImage 对象都填充了
image字段? -
我还没有任何 PostImage 对象。我希望能够有没有图片的帖子。