Reputation: 1447
I have a model that will have two similar "id"-like fields. One will be the primary key, one will be the a human-readable id.
On the backed/in the model, I have differentiated them as such
pk_id = models.AutoField(primary_key=True) # internal only, incrementing int
ext_id = models.CharField(max_length=128) # external facing, words+int
When the user does a query to get a list of the models, I want to EXCLUDE pk_id
entirely, and have ext_id
appear simply under the name id
. Like this:
{
'id' : 'name_title_001' # same as ext_id
'other_fields' : 'bla bla bla'
}
How do I accomplish this?
Upvotes: 0
Views: 33
Reputation: 629
suppose your model name is SomeModel; Update your serializers as
class SomeModelSerializer(serializers.ModelSerializer):
id = serializers.CharField(source='ext_id', read_only=True)
class Meta:
model = SomeModel
fields = (
'id',
# other fields excluding pk_id and ext_id
)
Upvotes: 1