1

假设我有:

模型.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
4

1 回答 1

1

我将回答我自己的问题,我们需要添加以下内容PersonResource

albums = fields.ToManyField('myapp.api.AlbumResource', 'album_set', full = True)

无需定义自定义方法或脱水。

于 2012-11-17T04:46:43.453 回答