Hello, It doesn’t really make sense to create a factory here. Django forms requires some overhead to handle the form management part whereas DRF’s serializers don’t need that. You should be able to write your own factory using Python’s type(name, bases, dict).
Regards, Xavier Ordoquy, Linovia. > Le 2 mai 2017 à 00:27, Aidan Lister <[email protected]> a écrit : > > 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] > <mailto:[email protected]>. > For more options, visit https://groups.google.com/d/optout > <https://groups.google.com/d/optout>. -- 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.
