171

I want to serialize a model, but want to include an additional field that requires doing some database lookups on the model instance to be serialized:

class FooSerializer(serializers.ModelSerializer):
  my_field = ... # result of some database queries on the input Foo object
  class Meta:
        model = Foo
        fields = ('id', 'name', 'myfield')

What is the right way to do this? I see that you can pass in extra "context" to the serializer, is the right answer to pass in the additional field in a context dictionary?

With that approach, the logic of getting the field I need would not be self-contained with the serializer definition, which is ideal since every serialized instance will need my_field. Elsewhere in the DRF serializers documentation it says "extra fields can correspond to any property or callable on the model". Are "extra fields" what I'm talking about?

Should I define a function in Foo's model definition that returns my_field value, and in the serializer I hook up my_field to that callable? What does that look like?

Happy to clarify the question if necessary.

Community
  • 1
  • 1
Neil
  • 6,236
  • 8
  • 37
  • 70

8 Answers8

256

I think SerializerMethodField is what you're looking for:

class FooSerializer(serializers.ModelSerializer):
  my_field = serializers.SerializerMethodField('is_named_bar')

  def is_named_bar(self, foo):
      return foo.name == "bar" 

  class Meta:
    model = Foo
    fields = ('id', 'name', 'my_field')

http://www.django-rest-framework.org/api-guide/fields/#serializermethodfield

suhailvs
  • 14,937
  • 8
  • 78
  • 88
J.P.
  • 2,676
  • 1
  • 10
  • 2
  • 22
    is it possible to add validation to such fields? my question is: how to accept custom POST values which can be validated and processes in the post_save() handler? – Alp May 30 '14 at 10:34
  • 25
    Note that SerializerMethodField is read-only, so this won't work for incoming POST/PUT/PATCH. – Scott A Aug 04 '15 at 19:12
  • 29
    In DRF 3, it is changed to `field_name = serializers.SerializerMethodField()` and `def get_field_name(self, obj):` – Chemical Programmer Dec 09 '15 at 13:25
  • 1
    whats the `foo` when def a SerializerMethodField ? when use CreateAPIView, is the foo have stored then can use the is_named_bar() method? – 244boy Nov 21 '17 at 08:01
  • So, based on this answer, if you want to pass say 12 extra fields to your serializer, you need to define 12 specific methods for each field that just returns foo.field_custom ? – AlxVallejo Apr 13 '18 at 19:13
  • 1
    "foo" here should be "instance" as it's the instance currently being 'seen' by the Serializer. – Tom O'Connor Oct 01 '20 at 19:29
  • Had to change it to obj. But thanks, it was super helpful – Shamsul Arefin Sajib Dec 30 '20 at 08:29
49

You can change your model method to property and use it in serializer with this approach.

class Foo(models.Model):
    . . .
    @property
    def my_field(self):
        return stuff
    . . .

class FooSerializer(ModelSerializer):
    my_field = serializers.ReadOnlyField(source='my_field')

    class Meta:
        model = Foo
        fields = ('my_field',)

Edit: With recent versions of rest framework (I tried 3.3.3), you don't need to change to property. Model method will just work fine.

chhantyal
  • 10,570
  • 6
  • 45
  • 71
  • Thanks @Wasil! I am not familiar with the use of properties in Django models, and cannot find good explanation of what it means. Can you explain? What is the point of the @property decorator here? – Neil Aug 23 '13 at 07:09
  • 2
    it means you can call this method like a property: i.e. `variable = model_instance.my_field` gives the same result as `variable = model_instance.my_field()` without the decorator. also: http://stackoverflow.com/a/6618176/2198571 – Wasil W. Siargiejczyk Aug 23 '13 at 07:36
  • 1
    This is not working, at least in Django 1.5.1 / djangorestframework==2.3.10. The ModelSerializer is not getting the propery even if explictly referred in "fields" Meta attribute. – ygneo Mar 23 '14 at 19:12
  • 9
    you need to add the field to the serializer because it's no *real* modelfield: my_field = serializers.Field(source='my_field') – Marius Apr 04 '14 at 09:00
  • 4
    `source='my_field' ` isn't require anymore and raise an exception – Guillaume Vincent Jun 30 '16 at 14:15
  • This technique doesn't work anymore. `my_field` won't be included in api response. I guess you need to use `SerializerMethodField` instead. – lapin Jan 01 '21 at 16:43
14

With the last version of Django Rest Framework, you need to create a method in your model with the name of the field you want to add. No need for @property and source='field' raise an error.

class Foo(models.Model):
    . . .
    def foo(self):
        return 'stuff'
    . . .

class FooSerializer(ModelSerializer):
    foo = serializers.ReadOnlyField()

    class Meta:
        model = Foo
        fields = ('foo',)
Guillaume Vincent
  • 9,586
  • 10
  • 66
  • 88
13

if you want read and write on your extra field, you can use a new custom serializer, that extends serializers.Serializer, and use it like this

class ExtraFieldSerializer(serializers.Serializer):
    def to_representation(self, instance): 
        # this would have the same as body as in a SerializerMethodField
        return 'my logic here'

    def to_internal_value(self, data):
        # This must return a dictionary that will be used to
        # update the caller's validation data, i.e. if the result
        # produced should just be set back into the field that this
        # serializer is set to, return the following:
        return {
          self.field_name: 'Any python object made with data: %s' % data
        }

class MyModelSerializer(serializers.ModelSerializer):
    my_extra_field = ExtraFieldSerializer(source='*')

    class Meta:
        model = MyModel
        fields = ['id', 'my_extra_field']

i use this in related nested fields with some custom logic

Arne Claassen
  • 13,120
  • 3
  • 58
  • 98
Marco Silva
  • 539
  • 5
  • 7
11

My response to a similar question (here) might be useful.

If you have a Model Method defined in the following way:

class MyModel(models.Model):
    ...

    def model_method(self):
        return "some_calculated_result"

You can add the result of calling said method to your serializer like so:

class MyModelSerializer(serializers.ModelSerializer):
    model_method_field = serializers.CharField(source='model_method')

p.s. Since the custom field isn't really a field in your model, you'll usually want to make it read-only, like so:

class Meta:
    model = MyModel
    read_only_fields = (
        'model_method_field',
        )
Community
  • 1
  • 1
Lindauson
  • 2,213
  • 1
  • 21
  • 28
3

This worked for me. If we want to just add an additional field in ModelSerializer, we can do it like below, and also the field can be assigned some val after some calculations of lookup. Or in some cases, if we want to send the parameters in API response.

In model.py

class Foo(models.Model):
    """Model Foo"""
    name = models.CharField(max_length=30, help_text="Customer Name")

In serializer.py

class FooSerializer(serializers.ModelSerializer):
    retrieved_time = serializers.SerializerMethodField()
    
    @classmethod
    def get_retrieved_time(self, object):
        """getter method to add field retrieved_time"""
        return None

  class Meta:
        model = Foo
        fields = ('id', 'name', 'retrieved_time ')

Hope this could help someone.

MLavoie
  • 8,799
  • 39
  • 36
  • 51
Vinay Kumar
  • 849
  • 8
  • 14
1
class Demo(models.Model):
    ...
    @property
    def property_name(self):
        ...

If you want to use the same property name:

class DemoSerializer(serializers.ModelSerializer):
    property_name = serializers.ReadOnlyField()
    class Meta:
        model = Product
        fields = '__all__' # or you can choose your own fields

If you want to use different property name, just change this:

new_property_name = serializers.ReadOnlyField(source='property_name')
0

As Chemical Programer said in this comment, in latest DRF you can just do it like this:

class FooSerializer(serializers.ModelSerializer):
    extra_field = serializers.SerializerMethodField()

    def get_extra_field(self, foo_instance):
        return foo_instance.a + foo_instance.b

    class Meta:
        model = Foo
        fields = ('extra_field', ...)

DRF docs source

trici0pa
  • 13
  • 4