-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automatic Inclusion of @Property Fields in ModelSerializer #9414
Conversation
@cclauss Could you please review this pull request? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DRF is net negetive in introducing new API surface, unless it is needed for Python or Django compatibility. so I am not sure if the other maintainers will accept this change. it would be nice you could share some example of true use cases that are used by most of the devs? it might be a good fit for some DRF extensions IMHO if the use case is very widely used.
@cclauss @auvipy Yes sure , I can share the exact use case and how this feature reduce the line of code below the example of that Consider i have model in django for storing complaint of users and the model look like this
by analyzing this model schema , we can see that there are three 3 property fields exist . To get these property fields in representation we can do using these following method .note these are the current django framework feature
by using the above method we will get the property fields in representation . if we are planning to use fields = "all" , writing fields name of model in serializer's fields variables it something wired if there are more fields in model . so with fields = 'all' we can do it by following method
here we have to add that manually using serializer method field by analyzing the two method we can see that adding the property fields taking more lines of code . To tackle this problem with adding property fields with single line we have to use new feature which i added . Using that feature we can add the property fields very easily and only 1 line is needed and it helps to reduce code line , complexity and reduce the error Now see the difference between doing the same job with new feature
Now you can see the change and effect of new feature in the scenario I faced this issue when i building project and i asked about this issue to my friends developers they also respond me positively I hope it clear well. i am sure that it definitely help whole the developers 💯 Thank you |
@auvipy can you please review it |
In my opinion, using |
But we can use tuples or list to mention the property fields rather than all This feature actually reducing the code line |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to close this as design not accepted. you are welcome to contribute on other issues which do not need to expose new API surface, unless otherwise needed by new django or python versions.
Overview
Backend developers often face the challenge of manually adding @Property fields to serializers using SerializerMethodField. This process can be repetitive and error-prone. To simplify and expedite the inclusion of @Property fields, we've introduced a new feature in the ModelSerializer that allows you to automatically include these fields with minimal configuration.
Feature Description
With this new feature, you can easily include @Property fields in your ModelSerializer by setting the property_fields attribute in the serializer's Meta class. You have the option to include all @Property fields or manually specify which ones to include.
Usage
Include All @Property Fields:
Set property_fields to all in the Meta class to automatically include all @Property fields.
Manually Specify @Property Fields:
Provide a list or tuple of @Property field names to include only specific fields.
Implementation
The feature is implemented in the get_fields method of the ModelSerializer. Here’s the relevant code:
Benefits
#Conclusion
This feature significantly enhances the efficiency of working with @Property fields in Django REST Framework serializers. By setting a single attribute in the Meta class, developers can now easily include these fields, making their code cleaner and more maintainable.
Feel free to use this feature in your projects and contribute any improvements or suggestions. Happy coding!