- Notifications
You must be signed in to change notification settings - Fork302
Filtering related fields with related_field#916
-
How can we filter the related fields |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 3 comments 3 replies
-
I took some time and now I have a solid thinking that we have to use the related views for related fields.
Currently what I do is this (not optimal and performant, but does the job):
This approach has things to consider:
I'll see if I can clean up the code and have others opinion. |
BetaWas this translation helpful?Give feedback.
All reactions
-
related urls served by a single viewset is a simple way of adding relationships links. It has its limitation though such as no filter support. Also important to note is that this got added later. The idea was to simplify the process of providing relationships links. BeforeRelationshipView was actually used for related urls and can still be used today. So if you wanna use filters better switch to With all the complexity and errors related urls served by a single viewset brought I wondered though whether it was a good idea to include this feature. Or maybe there is a better way of simplifying support of related urls then a need to configure each single relationship individuall per resources as currently done with |
BetaWas this translation helpful?Give feedback.
All reactions
-
I'm not sure if Still, I'm convinced in a way that related views should be severed by the respective view. I believe that#467 will be of a huge benefit here. |
BetaWas this translation helpful?Give feedback.
All reactions
-
Sorry I mixed up |
BetaWas this translation helpful?Give feedback.
All reactions
-
I took a look into those. This is kind of what I do now, with some extra magic. classBlogViewSet(ModelViewSet):queryset=Blog.objects.all()serializer_class=BlogSerializerdefget_queryset(self,*args,**kwargs):queryset=Blog.objects.all()# simplified# special function to call appropriate function based on kwargs passed to `re_path`,# and to check the permissions and queryset of the "parent" object (Entry for example)returnself.filter_queryset_for_relations(queryset ).annotate_read_counts()deffilter_queryset_for_Entry(self,queryset,instance):# called within filter_queryset_for_relations, instance is an Entryreturnqueryset.filter(entry_id=instance.id )deffilter_queryset_for_X(self,queryset,instance):# called within filter_queryset_for_relations, instance is an Xreturnqueryset.filter(# or return another queryset completely (instance.authors.all())x_id=instance.id ) The current issue is that the schema generation is wrong as it assumes that the entry pk is blog pk (description of |
BetaWas this translation helpful?Give feedback.
All reactions
-
I converted this issue to a discussion so it can be further discussed how to move forward or to leave it as it is. Issue can be extracted once there is an agreement on how to. |
BetaWas this translation helpful?Give feedback.
All reactions
This discussion was converted from issue #905 on April 16, 2021 20:12.