ModelSerializer and custom properties on Model

3,721 views
Skip to first unread message

Josh Mahoney

unread,
May 6, 2013, 5:48:35 PM5/6/13
to django-res...@googlegroups.com
Hi,

How can I support custom Model properties with ModelSerializer?

Say I have:

class Foo(models.Model):
... normal Django Model fields ...

@property
def Bar(self):
return "some stuff" 

How could I have 'Bar' in my list of fields for the ModelSerializer and have that work? (this is currently throwing a KeyError in serializers.py in get_fields) 

Tom Christie

unread,
May 7, 2013, 4:50:49 AM5/7/13
to django-res...@googlegroups.com
Hi Josh,

  You're looking for SerializerMethodField.  Documented here.

Cheers,

  Tom

Tom Christie

unread,
May 7, 2013, 5:35:41 AM5/7/13
to django-res...@googlegroups.com
Apologies, I should have read the post more carefully.
You should be able to add model properties by adding a field to the serializer.
In this case...

    Bar = serializers.Field()

Although I'd probably recommend using a more conventional lowercase style for your property name.



--
You received this message because you are subscribed to the Google Groups "django-rest-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Josh Mahoney

unread,
May 7, 2013, 1:39:03 PM5/7/13
to django-res...@googlegroups.com
Hi Tom,

Thanks for pointing me in the right direction... I missed this part of the documentation for some inexplicable reason. Anyway, I seem to have run into a bug here, although it is easily avoided. If the source attribute of Field is the same as the actual field name I get a KeyError. Filed here: https://github.com/tomchristie/django-rest-framework/issues/811

Best,
Josh


--
You received this message because you are subscribed to a topic in the Google Groups "django-rest-framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-rest-framework/DIYgleBhoIc/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to django-rest-fram...@googlegroups.com.

Josh Mahoney

unread,
May 7, 2013, 2:05:05 PM5/7/13
to django-res...@googlegroups.com, jo...@mahoney.com
Apologies - that issue I filed was incorrect. However, I am still experiencing a KeyError on 'extracted_data' in line 206 of rest_framework/serializers.py in get_fields. The following is what I have and I can't see why it isn't working:

# serializers.py
class MasterSerializer(serializers.ModelSerializer):
    class Meta:
        model = Master
        extracted_data = serializers.Field(source='get_tabs_meta')
        statistics = serializers.Field()
        fields = ('id', 'sha256', 'file_size', 'file_type', 'file_subtype', 'extracted_data', 'valid', 'validated', 'statistics')

# models.py
class Master(models.Model):
    id = models.BigIntegerField(primary_key=True)
    sha256 = BlobField(unique=True)
    file_size = models.BigIntegerField()
    file_type = models.CharField(max_length=512)
    file_subtype = models.CharField(max_length=512)
    meta = BlobField()
    stats = BlobField()
    valid = models.BooleanField()
    validated = models.BooleanField()
    _tc_meta = None
    _stat_meta = None

    class Meta:
        db_table = u'master'

    def tc_meta(self):
        if self._tc_meta is None:
            tc_meta = meta_pb2.metadata_t()
            tc_meta.ParseFromString(base64.b16decode(self.meta))
            self._tc_meta = protobuf_json.pb2json(tc_meta)
        return self._tc_meta
    tc_meta.allow_tags = True

    def statistics(self):
        if self._stat_meta is None:
            stat_meta = stat_pb2.statistics_t()
            stat_meta.ParseFromString(base64.b16decode(self.stats))
            self._stat_meta = protobuf_json.pb2json(stat_meta)
        return self._stat_meta

    def get_tabs_meta(self):
        tm = self.tc_meta()
        return tm

# views.py
class MasterDetail(generics.RetrieveAPIView):
    queryset = Master.objects.all()
    lookup_field = 'sha256'
    serializer_class = MasterSerializer
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to a topic in the Google Groups "django-rest-framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-rest-framework/DIYgleBhoIc/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to django-rest-framework+unsub...@googlegroups.com.

Josh Mahoney

unread,
May 7, 2013, 2:21:48 PM5/7/13
to django-res...@googlegroups.com, jo...@mahoney.com
Bah!! I'm an idiot. I had the fields under the "Meta" class, instead of at the MasterSerializer class level. Sometimes staring at something makes you blind. :) Problem fixed... carry on... nothing to see here...
Reply all
Reply to author
Forward
0 new messages