所以我的模特中有这个:
class narative(models.Model):
voice = models.FileField(upload_to='uploads/%Y/%m/%d',blank = True, null=True)
def filename(self):
return os.path.basename(self.voice.name)
def clean(self):
return os.path.basename(self.voice.name)
def __unicode__(self):
return self.filename()
class geolocations(models.Model):
name = models.CharField(max_length=255)
parent = models.ForeignKey('self', blank=True,null=True)
sort = models.IntegerField(default=0, blank=True,null=True)
description = HTMLField()
locations = models.ManyToManyField('locgeo')
excerpt = models.CharField(max_length=4096)
icon = models.CharField(max_length=255, blank = True, null=True)
tags = models.CharField(max_length=255, blank = True, null=True)
categories = models.ManyToManyField('categories',blank = True, null=True)
photos = models.ManyToManyField('Photo', blank = True, null=True)
narations = models.ManyToManyField('narative', blank = True, null=True)
然后在我的api.py
中class NarRes(ModelResource):
class Meta:
queryset = narative.objects.all()
resource_name = 'nar'
class EntryResource(ModelResource):
locations = fields.ToManyField('self', filter_locations_per_bundle, full=True, null=True)
locgeo = fields.ToManyField('myapp.api.LocGeoRes', 'locations', full=True, null=True)
photos = fields.ToManyField('myapp.api.photoResource','photos', full=True, null=True)
narative = fields.ToManyField('myapp.api.NarRes','narative', full=True, null=True)
class Meta:
queryset = geolocations.objects.all()
resource_name = 'Locations'
fields = ['parent','narative','id','name','description','excerpt','lgeo','icon','tags','categories','photos','locations']
问题是,json输出将narative列为空数组:
...
locations: [ ],
locgeo: [
{
id: 1,
latitude: "33",
longitude: "22",
name: "Location",
radius: 140,
resource_uri: "/api/v1/lgeo/1/"
}
],
name: "Location",
narative: [ ],
photos: [
{
id: 6,
name: "Location",
photo: "/media/uploads/2014/04/18/IMG_1643_result.JPG",
resource_uri: "/api/v1/photos/6/"
},
...
当我知道该记录有一个条目时(我可以在列出比较资源时看到它)。
我做错了什么?
答案 0 :(得分:0)
如模型中所示,外键的名称是“narations”。 在我的资源中,我将其称为“narative”。
修改后的行应为:
narative = fields.ToManyField('myapp.api.NarRes','narations', full=True, null=True)
修正了它。