5

I have a form with fields which are not associated with a model. I assume to implement the equivalent using a REST API (django-rest-framework), I would have to pass those additional fields, which are not associated with a Serializer? How do I do that?

Let's say the additional field is number_of_pages. I use that for some calculation. How do I allow that to be passed in my REST call?

2
  • 1
    which Rest frame work are you using django-tastypie django-rest-framework or some other frame work? If you are using django-tastypie then hydrate method is what you are looking after
    – Pavan
    May 24, 2016 at 11:33
  • django-rest-framework
    – nilanjan
    May 26, 2016 at 8:56

2 Answers 2

3

if you are using ModelSerializer from DjangoRestFramework, just add a field.

by default only model fields are added, but nothing limits you to add more, the only thing that may be problematic (but I've not tested it) - you may have too many fields while creating or updating model - in such a case, you will need to remove those fields in create() and update() methods before calling save().

class MyModelSerializer(serializers.ModelSerializer):
    number_of_pages = fields.IntegerField()

    # this I'm not sure if needed
    def create(self, validated_data):
        validated_data.pop('number_of_pages')
        return super(MyModelSerializer, self).create(validated_data)

    def update(self,instance, validated_data):
        validated_data.pop('number_of_pages')
        return super(MyModelSerializer, self).update(instance, validated_data)
    # end

    class Meta:
        fields = ('mymodelfield_1', 'mymodelfield_2', 'number_of_pages')
        model = MyModel
3
  • This fields is from django.db.models?
    – Caco
    Oct 20, 2017 at 17:48
  • It seems to missed something. I get error: django.core.exceptions.ImproperlyConfigured: Field name 'number_of_pages' is not valid for model 'MyModel'.
    – Caco
    Oct 20, 2017 at 18:09
  • Ok, it works. In my case, however, the extra field is optional, so I added required=False in the declaration of the extra field, like would be in number_of_pages = fields.IntegerField(required=False), but the field type is boolean, so specifically for me, I did default=fields.BooleanField(required=False). Don't doing this I get error: 'QuestionnaireLanguage' object has no attribute 'is_default'.
    – Caco
    Oct 23, 2017 at 18:23
0

If, you are using django-rest-framework, then you can use SerializerMethodField() to pass additional fields in Api. Below I give an example.

class UserGroupSerializer(serializers.ModelSerializer):
"""
    This Serializer pass additionl field count. by using SerializerMethodField()
"""
count = serializers.SerializerMethodField()

class Meta:
    model = UserGroup

def get_count(self,obj):
    """return length of group"""
    return Groupmember.objects.filter(user_group=obj.id).count()

and use

serializer_class = UserGroupSerializer

in view.

I think it can help you.

Thanks

3
  • How will I pass the data if I call the rest api? What will the json look like?
    – nilanjan
    May 26, 2016 at 8:55
  • you are using djnago-rest-framework ?. and what your requirement. If you are using django-rest-framework please look into this link. django-rest-framework.org/tutorial/quickstart May 26, 2016 at 10:10
  • which data you want to pass in api call ? May 26, 2016 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.