假设我有:
模型.py:
class Person(models.Model):
user = models.OneToOneField(User)
def get_album(self):
return self.album_set.all()
class Album(models.Model):
user = models.ForeignKey(Person)
photo = models.ImageField(upload_to = 'blahblah')
api.py:
class PersonResource(ModelResource):
class Meta:
queryset = Person.objects.all()
class AlbumResource(ModelResource):
class Meta:
queryset = Album.objects.all()
我怎样才能使一个字段脱水,PersonResource
所以它的值是.get_album()
?
我试图像这样使用 ToManyField :
albums = fields.ToManyField(readonly = True)
但它抛出:
Exception Type: TypeError
Exception Value:
__init__() takes at least 3 arguments (2 given)
Exception Location: C:\PHOTOBLOG\photoblog\person\api.py in PersonResource, line 8