-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Fixes: #17976 DeviceType_Count on Manufacturer #18583
base: main
Are you sure you want to change the base?
Fixes: #17976 DeviceType_Count on Manufacturer #18583
Conversation
@renatoalmeidaoliveira it would be ideal to solve this by enabling recursion through nested serializers inside |
@jeremystretch that was my first try, i.e. using |
About using Using
|
About changing
Running with this with Debug it executes the following query:
And looking at the results inside Django debug, its returning the correct results but as an attribute of So even the query returning the expected results the JSON output omits the result instead of adding it inside the Nested Object |
netbox/utilities/api.py
Outdated
@@ -87,6 +88,7 @@ def get_prefetches_for_serializer(serializer_class, fields_to_include=None): | |||
fields_to_include = serializer_class.Meta.fields | |||
|
|||
prefetch_fields = [] | |||
annotaded_prefetch = {} |
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.
minor spelling issue, annotated
would be correct spelling
netbox/utilities/api.py
Outdated
# Add an annotation to the annotaded_prefetch | ||
if isinstance(serializer_field, RelatedObjectCountField) and source_field is not None: | ||
if model_field_name not in annotaded_prefetch: | ||
annotaded_prefetch[model_field_name] = Count(serializer_field.relation) |
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.
Do we need to continue processing at this point? I think we can just do a continue here?
netbox/utilities/api.py
Outdated
# If the serializer field does not map to a discrete model field, skip it. | ||
try: | ||
field = model._meta.get_field(model_field_name) | ||
if isinstance(field, (RelatedField, ManyToOneRel, GenericForeignKey)): | ||
if (isinstance(field, (RelatedField, ManyToOneRel, GenericForeignKey)) and |
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.
This can be moved below the try/except - if the field doesn't exist we are doing a continue so it won't get hit and field will be set.
Also - if it is issubclass(type(serializer_field), Serializer)
do we want to continue to line 117? Not sure - just asking, could we just do a check on that condition and continue?
@renatoalmeidaoliveira is this ready for re-review? If you do some fixes please click the re-review button as otherwise it doesn't pop up in our queues. |
@arthanson it's not ready yet... I tryed to use a different approach based on your comments and @jeremystretch's but it breaks a lot of things for other models, gonna post some code snippet about that sollution |
Fixes: #17976 DeviceType_Count on Manufacturer
get_prefetches_for_serializer
to support nested serializersRelatedObjectCountField
fields and create aPrefetch
with the correct annotation