models.py:valore di chiave esterna in Django REST quadro
class Station(models.Model):
station = models.CharField()
class Flat(models.Model):
station = models.ForeignKey(Station, related_name="metro")
# another fields
Poi nel serializers.py:
class StationSerializer(serializers.ModelSerializer):
station = serializers.RelatedField(read_only=True)
class Meta:
model = Station
class FlatSerializer(serializers.ModelSerializer):
station_name = serializers.RelatedField(source='station', read_only=True)
class Meta:
model = Flat
fields = ('station_name',)
E ho un errore:
NotImplementedError:
RelatedField.to_representation()
must be implemented. If you are upgrading from REST framework version 2 you might wantReadOnlyField
.
I read this , but it does not help me.
How to fix that?
Thanks!
Perché stai utilizzando RelatedField per il campo Station.station? Sicuramente dovrebbe essere CharField. –
@DanielRoseman con '' 'station = serializers.CharField (read_only = True)' '' lo stesso problema – tim