2
\$\begingroup\$

Can some one help me on how to eliminate repetition to reduce duplication score on the below classes created in the code

class EnquiryFilterSerializer(serializers.ModelSerializer):
    class Meta:
        model = enquirylog
        fields = '__all__'


class EnquirySerializer(serializers.ModelSerializer):
    class Meta:
        model = enquirylog
        fields = '__all__'

    def update(self, instance, validated_data):
    # making sure if broker is not fetched then replace null value with
    # some resonable data as it will help in duing filter
        if len(validated_data.get('email_broker', instance.email_broker)) == 0:
            instance.email_broker = not_avbl

        instance.save()
        return instance


class EnquiryBrokerNameSerializer(serializers.ModelSerializer):
    class Meta:
        model = enquirylog
        fields = ['broker_email_id']


class EnquiryAssignedUserSerializer(serializers.ModelSerializer):
    class Meta:
        model = enquirylog
        fields = ['email_assigned_user']


class EnquiryInsuredNameSerializer(serializers.ModelSerializer):
    class Meta:
        model = enquirylog
        fields = ['ef_insured_name']


class EnquiryObligorNameSerializer(serializers.ModelSerializer):
    class Meta:
        model = enquirylog
        fields = ['ef_obligor_name']


class EnquiryCountryNameSerializer(serializers.ModelSerializer):
    class Meta:
        model = enquirylog
        fields = ['el_country_name']


class EnquiryLimitSerializer(serializers.ModelSerializer):
    class Meta:
        model = enquirylog
        fields = ['enquirylog_limit']


class EnquiryDecisionSerializer(serializers.ModelSerializer):
    class Meta:
        model = enquirylog
        fields = ['ef_underwriter_decision']


class NotesSerializer(serializers.ModelSerializer):
    class Meta:
        model = notes
        fields = '__all__'
\$\endgroup\$
1
  • \$\begingroup\$ The current question title, which states your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How do I ask a good question?. \$\endgroup\$
    – BCdotWEB
    Jun 30, 2021 at 7:22

1 Answer 1

2
\$\begingroup\$

type is just another type. Classes can be created in, and returned from, functions just fine. An approach kind of like

def basic_model_serializer(model, fields):
    _model, _fields = model, fields

    class _internal(serializers.ModelSerializer):
        class Meta:
            model = _model
            fields = _fields

    return _internal

EnquiryFilterSerializer = basic_model_serializer(enquirylog, '__all__')
EnquiryBrokerNameSerializer = basic_model_serializer(enquirylog, ['broker_email_id'])
...
NotesSerializer = basic_model_serializer(notes, '__all__')

class EnquirySerializer(serializers.ModelSerializer):
    ... # unchanged due to update()

would let you abstract over the few differences of the most similar classes in a pretty natural way. Is it an improvement over your existing code? Maybe in some ways, maybe not in others.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ I am getting error saying Unresolved reference to 'model' and Unresolved reference to 'fields' \$\endgroup\$
    – Sherlock
    May 31, 2021 at 6:35
  • 1
    \$\begingroup\$ @Sherlock Right, sorry about that, I got it too but forgot to put my workaround in the answer \$\endgroup\$
    – Sara J
    May 31, 2021 at 6:45
  • 1
    \$\begingroup\$ Thank you I resolved the errors \$\endgroup\$
    – Sherlock
    May 31, 2021 at 13:48

Your Answer

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

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