1

私はこのようなモデルを得ま​​した:

from django.db import models
from django_countries.fields import CountryField

class Location(models.Model):
    company = models.CharField(max_length=64)
    country = CountryField()

    def __unicode__(self):
        return self.company

現在、API に TastyPie を使用しています。このような非常に単純なモデルを取得しました (以前にフィルターとフィールドで編集したことがありますが、成功しませんでした)。

class LocationResource(ModelResource):
    class Meta:
        queryset = Location.objects.all()
        resource_name = 'location'

返されるもの:

{"company": "testcompany", "country":"DE" "resource_uri": "/api/location/1/"}

私が必要とするのは、国名、またはさらに良いことに、国フィールドからのものです。

4

3 に答える 3

1

の脱水方法を追加できcountryますLocationResource

def dehydrate_country(self, bundle):
    return bundle.obj.country.name 

OR

DRFインスタンス化を使用してCountry fieldいる場合

 from django_countries.serializer_fields import CountryField

 country = CountryField(country_dict=True)

あなたのserializer

于 2016-03-18T10:35:57.003 に答える