Is there a factory pattern for serializers, ala modelform_factory or 
modelformset_factory?

I end up doing this type of stuff a lot, e.g. a full serializer, a lite 
serializer, etc. it'd be cool to do either: BranchListSerializer = 
serializerfactory(BranchSerializer, fields=('id', 'name'))


class BranchSerializer(serializers.ModelSerializer):
    property_count = serializers.IntegerField(read_only=True)
    current_business_hours = serializers.SerializerMethodField()


    def get_current_business_hours(self, obj):
            in_bh, bh = obj.get_business_hours_for_date()
            return {
                'in_bh': in_bh,
                'hours': bh,
                'is_set': bool(obj.business_hours),
            }


    included_serializers = {
        'groups': 
'abas.apps.accounts.api.v2.serializers.UserGroupSerializer',
        'user_set': 'abas.apps.accounts.api.v2.serializers.UserSerializer',
    }


    class Meta:
        model = Branch
        fields = (
            'id',
            'name',
            'code',


            # relationships
            'groups',
            'property_count',
            'user_set',


            # methods
            'get_absolute_url',
            'current_business_hours',
        )




class BranchNameSerializer(BranchSerializer)
    class Meta:
        model = Branch
        fields = (
            'id',
            '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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to