我有一个桩模型和postImage型号:Django的tastypie外键的麻烦
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()
我创建tastypie资源两种:
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.
我还没有任何PostImages呢。
如果我摆脱images = fields.foreignKey
线PostResource
,它的工作原理。如果我浏览到api/v1/postImage/?format=json
,则不显示错误,只是一个空集合。
你是否检查过你的所有PostImage对象都填充了'image'字段? – Lycha 2012-07-12 21:08:17
我还没有任何PostImage对象。我希望能够在没有图像的情况下发布帖子。 – AlexBrand 2012-07-12 21:20:23