You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi there, I dont see a good way to do this the documentation, but lets say I have a filter that is conditionally based, say on a permission. For example lets say the user of the filter has a particular flag on the user model. Here's some code for example.
class ReportsListFilter(filters.FilterSet):
date_history = filters.DateFilter('complete_utc__gte', label="Date History")
class Meta:
model = PrintQueue
fields = ['household', 'member', 'date_history']
def date_history_gte(self, queryset, name, value):
date = datetime.now() - timedelta(days=int(value))
return queryset.filter(complete_utc__gte=date)
class ReportsList(generics.ListCreateAPIView):
serializer_class = ReportsListSerializer
authentication_classes = (...)
permission_classes = (...)
filterset_class = ReportsListFilter
def get_queryset(self):
return PrintQueue.objects.filter(member=self.request.user).order_by('-id')
Lets say that that date_history field, or really any field is conditionally based. Should I create a different class and determine the filterset_class based on said condition? Or can I do it in the ReportsListFilter?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi there, I dont see a good way to do this the documentation, but lets say I have a filter that is conditionally based, say on a permission. For example lets say the user of the filter has a particular flag on the user model. Here's some code for example.
Lets say that that date_history field, or really any field is conditionally based. Should I create a different class and determine the
filterset_class
based on said condition? Or can I do it in theReportsListFilter
?Beta Was this translation helpful? Give feedback.
All reactions