I am trying to change Model field name in DRF Serializer like alias in SQL. I have tried different methods but cannot succeed.
models.py
class Park(models.Model):
name = models.CharField(max_length=256)
alternate_name = models.CharField(max_length=256, blank=True)
objects = models.GeoManager()
class Meta:
db_table = u'p_park'
def __unicode__(self):
return '%s' % self.name
def alias_alternate_name(self):
return self.alternate_name
serializers.py
class ParkSerializer(serializers.ModelSerializer):
location = serializers.Field(source='alias_alternate_name')
#location = serializers.SerializerMethodField(source='alias_alternate_name')
#alternate_name as location
class Meta:
model = Park
fields = ('id', 'name', 'location')
I have also tried to add alias in Django Queryset but cannot changed.
Updated
This is the exception that i am facing
AttributeError at /ViewName/ 'module' object has no attribute 'Field'
How can I do this?
serializers.SerializerMethodField
approach? I mean this:serializers.SerializerMethodField('get_location')
anddef get_location(self, obj): ...
– Ankledeepserializers.py
? – Lobster