私はhttps://github.com/coderholic/django-citiesを使用しています。私はシリアライザに都市と国を追加したいと考えていました。Django <object>はJSONシリアライズ可能ではありません
これは私のモデルである:私はそれは私が取得しています動作するかどうかを確認しようとしているときに
class LocationsView(generics.ListAPIView):
queryset = Location.objects.order_by('-id')
serializer_class = LocationsSerializer
serializers.py
class LocationsSerializer(serializers.ModelSerializer):
country = serializers.ReadOnlyField(source='city.country')
class Meta:
model = Location
fields = ['id', 'name', 'geolocation', 'city', 'country']
:
from cities.models import Country, City
class Location(models.Model):
name = models.CharField(max_length=200, blank=True, null=True, default=None)
city = models.ForeignKey(City, blank=True, null=True, default=None, related_name='city_of_location')
geolocation = map_fields.GeoLocationField(max_length=100, blank=True, default='')
私の見解:
<Country: Austria> is not JSON serializable
はどのように「それが動作するかどうか見て」いますか? –
GeoJSONシリアライザが必要なのでしょうか? https://docs.djangoproject.com/en/1.11/ref/contrib/gis/serializers/それがあなたとあなたのrequirements.txtと呼ぶ方法を見る助けになります –
@DanielRoseman私はhttp://127.0.0.1にアクセスしています:8000/locations /これは私がjsonを見なければならない場所です。シリアライザでシティーと国を追加するまではすべて動作しました –