-
Notifications
You must be signed in to change notification settings - Fork 116
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
Contributing a Different/Complementary Approach #57
Comments
We are looking into making our own logic to handle this as well. Would be great to have a solution built in. |
Our implementation is available at my fork. This repo is built on
If that sounds OK for your situation, replace any existing mixins with the new ones:
If you need a specific feature integrated, create a ticket and I'll let you know if it's something I can quickly take care of. |
Hello @claytondaley! Thank you for the interesting thoughts. Your approach is in style of others drf fields and it looks flexible. I think it will be good if we can implement both approaches, the existing by default and your when we need more flexible control on the child serializer. I’ll try to research this area in free time. |
Hi @claytondaley thank you for your input. We discussed your proposal and decided that it make sense to support both approaches. Please send the merge request, we will review it and then merge. Best regards, |
Great. I'm actually taking the time to open source it (with permission) so I can use it in some other projects of mine. I'm still finding/cleaning up little issues (like handling |
Just a quick update. I made some architectural changes to better align my approach with the existing code base. I've been using my code in a side project and have been resolving issues as I go. From a code standpoint, I'm close to trying to combine the two approaches, but it won't happen quickly in calendar-time unless someone else is trying to use it and runs into an issue that can only be resolved by prioritizing the effort. |
In case someone wants/needs to try it out, here's the (public) side project where my new code is in use: The example not only shows the top-level get-or-create but an example of nesting GetOrCreate serializers to an arbitrary depth across both forward- and reverse-relations. |
Background: Before open sourcing our DIY nested writable logic, I wanted to look for existing package(s) -- to avoid fragmenting the users/contributors. I've been digging through this codebase to evaluate it as an alternative to our existing package. My conclusion is that it's missing a key feature (that we use extensively) due to a difference in design.
The Feature: If I follow the logic in this package, it achieves a get-or-create behavior by having the parent serializer lookup related objects by
pk
(or another package-wide name). Because the parent serializer does the lookup, it's impossible to look up related objects by non-PK fields (e.g. #12) unless you reconfigure the entire project to treat that field as the "pk field".The Opportunity: Our approach delegates this behavior to the nested serializer. The nested serializer takes the data and returns a model instance (or list of instances) based on how its configured. This make it possible to configure each nested writable serializer with different behaviors. For example:
pk
,name
, a composite key like['field1', 'field2']
, or even exact matches using__all__
To better illustrate the difference, consider a usage example:
An obvious advantage of your approach is that there's less configuration. It's theoretically possible to have dozens of nested serializers and mix the
GetOrCreate
mixin into the one parent class. At the same time, we need theUniqueFieldsMixin
on most (all?) our child serializers; at that point the difference is pretty trivial.If this package is ever going to support per-nested-serializer configurations, it will need to adopt an approach like this. To avoid creating a competing package, I'd be willing to take the time to get it integrated as a backwards-compatible extension to this package. Either way, I need to add a bunch of the bells-and-whistles found in your package so the "incremental" effort is mostly around backwards compatibility.
Obviously, this isn't a trivial change so I wanted to get buy-in before investing any time into it.
The text was updated successfully, but these errors were encountered: