django-filter not showing filtered items - django

I used django-filter in a similar way before and it worked fine, but now when I try to filter my posts it just returns all the items instead of filtering them, can anyone figure what I'm doing wrong?
my filters.py
class filtering(django_filters.FilterSet):
class Meta:
model = Cars
fields = ['color','body']
and my views:
def peugeot_206(request):
### Getting Last Month Records of 206 ###
start_date = (datetime.today() - timedelta(30))
end_date = (datetime.today() + timedelta(1))
last_month_records = Cars.objects.filter(datetime__range=(start_date, end_date),car='206')
post_list = last_month_records.order_by('-datetime').values()
### Calculating Aggregations ###
average_price = last_month_records.aggregate(Avg('price'))
min_price = last_month_records.aggregate(Min('price'))
max_price = last_month_records.aggregate(Max('price'))
filters = filtering(request.GET,queryset=post_list)
post_list = filters.qs.values()
### Pagination ###
paginator = Paginator(post_list,10)
page_number = request.GET.get('page')
posts = paginator.get_page(page_number)
return render(request,'main.html',
{
'average_price':average_price,
'min_price': min_price,
'max_price':max_price,
'posts':posts,
'paginator':paginator,
'filters':filters
})
UPDATE: it works when I set multiple filters, but when I set a single filter it still shows all the items
UPDATE 2: it works fine when I remove datetime__range=(start_date, end_date) from my object filter, does anyone know why ? seems to be a bug

Related

Flask pagination, query issue, why so many items?

I'm workin on a blog page and I'm trying to filter posts by tag, the problem is that I get several pages when only 1 or 2 posts match the query (I have per_page set to 6).
I have another filter by followed posts that works correctly, therefore I guess the problem is in the query object.
This is part of my code for the view index:
if current_user.is_authenticated:
show_followed = bool(request.cookies.get('show_followed', ''))
if show_followed:
query = current_user.followed_posts
elif show_tag:
tag = Tag.query.filter_by(tag_name=show_tag).first()
query = tag.post
else:
query = Post.query
page = request.args.get('page', 1, type=int)
pagination = query.order_by(Post.timestamp.desc()).paginate(
page, per_page=current_app.config['POSTS_PER_PAGE'],
error_out=False)
posts = pagination.items
return render_template("index.html", posts=posts, pagination = pagination, show_followed=show_followed, show_tag=show_tag)
when I try to replicate the issue in the console, I check that the query for a sample tag matches 3 posts, nevertheless pagination.total (total items) returns 23! What does the pagination object take as items??
I did this to test in the flask shell:
tagname = 'pruebatag'
tag = Tag.query.filter_by(tag_name=tagname).first()
query = tag.post
#here if y try query.all() I get 3 posts in return
pag_object = query.paginate(per_page = 6)
pag_object.total
>>> 23
pag_object.pages
>>> 4
pag_object.items #returns items for current page
>>> [<Post example>] #only one post returned for this page? why 4 pages?
I copy my definition of the tags table:
class Tag(db.Model):
__tablename__ = 'tags'
id = db.Column(db.Integer, primary_key=True)
tag_name = db.Column(db.String(40), unique=True)
def add_tag(self):
if not self.id:
db.session.add(self)
db.session.commit()
tag_join = db.Table('tag_join',
db.Column('post_id', db.Integer, db.ForeignKey('post.id')),
db.Column('tag_id', db.Integer, db.ForeignKey('tags.id'))
)
And in the post model:
tags = db.relationship('Tag',
secondary=tag_join,
backref=db.backref('post', lazy='dynamic'),
lazy='dynamic')
Any help would be highly appreciated as I am wasting a lot of time on this issue
Just let mi know if you need more details.
Thanks!
Finally I found what happens, I realize that table tag_join is full of duplicates, that cause the issue.

Django merge querysets in one page

In django rest framework i am using pagination of 20 on a model.
Now i want to make a queryset which takes 5 items which has the featured field as true.
The other 15 should be items which has the featured field as false.
Does anyone have an idea how to make this work?
permission_classes = [AllowAny]
serializer_class = JobSerializer
pagination_class = Pagination
order_types = {
1: 'job_order', #by randomized job order
2: '-created_date', #newest to oldest
3: 'created_date', #oldest to newest
4: 'name', #by name A -> Z,
5: '-name' #by name Z -> A,
}
def get_queryset(self):
requirements = self.request.query_params.getlist('requirements', None)
order_param = self.request.query_params.get('order_by', 1)
#make querysets
queryset_featured = Job.objects.all().filter(active=True, featured=True).order_by(self.order_types[int(order_param)])
queryset_non_featured = Job.objects.all().filter(active=True, featured=False).order_by(self.order_types[int(order_param)])
return queryset.distinct()
Use a union of two separate queries:
https://docs.djangoproject.com/en/3.1/ref/models/querysets/#union
This will allow you to make one ORM query for the featured items with [0:5], and then union to an ORM query for non-featured items with [0:15].
I haven't tested this, but here's an example with your code:
queryset_featured = Job.objects.all().filter(active=True, featured=True).order_by(self.order_types[int(order_param)])[0:5]
queryset_non_featured = Job.objects.all().filter(active=True, featured=False).order_by(self.order_types[int(order_param)])[0:15]
queryset_combined = queryset_featured.union(queryset_non_featured)
for row in queryset_combined:
print(row)

Django filter exact match for multi field: ManyToManyField using ModelMultipleChoiceFilter

I'm using Django filters (django-filter) in my project. I have the models below, where a composition (Work) has a many-to-many instrumentations field with a through model. Each instrumentation has several instruments within it.
models.py:
class Work(models.Model):
instrumentations = models.ManyToManyField(Instrument,
through='Instrumentation',
blank=True)
class Instrument(models.Model):
name = models.CharField(max_length=100)
class Instrumentation(models.Model):
players = models.IntegerField(validators=[MinValueValidator(1)])
work = models.ForeignKey(Work, on_delete=models.CASCADE)
instrument = models.ForeignKey(Instrument, on_delete=models.CASCADE)
views.py:
import django_filters
class WorkFilter(django_filters.FilterSet):
instrument = django_filters.ModelMultipleChoiceFilter(
field_name="instrumentation__instrument",
queryset=Instrument.objects.all())
My filter works fine: it grabs all the pieces where there is the instrument selected by the user in the filter form.
However, I'd like to add the possibility of filtering the compositions with those exact instruments. For instance, if a piece contains violin, horn and cello and nothing else, I'd like to get that, but not a piece written for violin, horn, cello, and percussion. Is it possible to achieve that?
I'd also like the user to choose, from the interface, whether to perform an exact search or not, but that's a secondary issue for now, I suppose.
Update: type_of_search using ChoiceFilter
I made some progress; with the code below, I can give the user a choice between the two kinds of search. Now, I need to find which query would grab only the compositions with that exact set of instruments.
class WorkFilter(django_filters.FilterSet):
# ...
CHOICES = {
('exact', 'exact'), ('not_exact', 'not_exact')
}
type_of_search = django_filters.ChoiceFilter(label="Exact match?", choices=CHOICES, method="filter_instruments")
def filter_instruments(self, queryset, name, value):
if value == 'exact':
return queryset.??
elif value == 'not_exact':
return queryset.??
I know that the query I want is something like:
Work.objects.filter(instrumentations__name='violin').filter(instrumentations__name='viola').filter(instrumentations__name='horn')
I just don't know how to 'translate' it into the django_filters language.
Update 2: 'exact' query using QuerySet.annotate
Thanks to this question, I think this is the query I'm looking for:
from django.db.models import Count
instrument_list = ['...'] # How do I grab them from the form?
instruments_query = Work.objects.annotate(count=Count('instrumentations__name')).filter(count=len(instrument_list))
for instrument in instrument_list:
instruments_query = instruments_query.filter(instrumentations__name=instrument_list)
I feel I'm close, I just don't know how to integrate this with django_filters.
Update 3: WorkFilter that returns empty if the search is exact
class WorkFilter(django_filters.FilterSet):
genre = django_filters.ModelChoiceFilter(
queryset=Genre.objects.all(),
label="Filter by genre")
instrument = django_filters.ModelMultipleChoiceFilter(
field_name="instrumentation__instrument",
queryset=Instrument.objects.all(),
label="Filter by instrument")
CHOICES = {
('exact', 'exact'), ('not_exact', 'not_exact')
}
type_of_search = django_filters.ChoiceFilter(label="Exact match?", choices=CHOICES, method="filter_instruments")
def filter_instruments(self, queryset, name, value):
instrument_list = self.data.getlist('instrumentation__instrument')
if value == 'exact':
queryset = queryset.annotate(count=Count('instrumentations__name')).filter(count=len(instrument_list))
for instrument in instrument_list:
queryset = queryset.filter(instrumentations__name=instrument)
elif value == 'not_exact':
pass # queryset = ...
return queryset
class Meta:
model = Work
fields = ['genre', 'title', 'instrument', 'instrumentation']
You can grab instrument_list with self.data.getlist('instrument').
This is how you would use instrument_list for the 'exact' query:
type_of_search = django_filters.ChoiceFilter(label="Exact match?", choices=CHOICES, method=lambda queryset, name, value: queryset)
instrument = django_filters.ModelMultipleChoiceFilter(
field_name="instrumentation__instrument",
queryset=Instrument.objects.all(),
label="Filter by instrument",
method="filter_instruments")
def filter_instruments(self, queryset, name, value):
if not value:
return queryset
instrument_list = self.data.getlist('instrument') # [v.pk for v in value]
type_of_search = self.data.get('type_of_search')
if type_of_search == 'exact':
queryset = queryset.annotate(count=Count('instrumentations')).filter(count=len(instrument_list))
for instrument in instrument_list:
queryset = queryset.filter(instrumentations__pk=instrument)
else:
queryset = queryset.filter(instrumentations__pk__in=instrument_list).distinct()
return queryset

filtering a queryset, applying more than one filter

My models are:
models.User:
id = pk
username = text
models.Offer
id = pk
description = text
publicationDate = Date
user = Fk(User)
my serializer is:
class UserOfferSerializer(ModelSerializer):
offers = OfferSerializerAll(many=True, read_only=True)
class Meta:
model = User
fields = ('id', 'username', 'offers')
I am trying to apply more than one filter on the queryset:
users = users.filter(offers__publicationDate__range=[startdate, enddate]). prefetch_related(Prefetch('offers', queryset=Offer.objects.filter(
publicationDate__range=[startdate, enddate]))).distinct()
then
users = users.filter(offers__description__icontains=sometext).prefetch_related(Prefetch('offers', queryset=Offer.objects.filter(description__icontains=sometext))).distinct()
First one works fine and the other one throws the following exception:
ValueError: 'offers' lookup was already seen with a different queryset. You may need to adjust the ordering of your lookups.
Update:
My current code is:
if (offerBeginDate != None and offerEndDate != None):
b = offerBeginDate.split('-')
e = offerEndDate.split('-')
startdate = datetime.date(int(b[0]), int(b[1]), int(b[2]))
enddate = datetime.date(int(e[0]), int(e[1]), int(e[2]))
users = users.filter(offers__publicationDate__range=[startdate, enddate])
offers = offers.filter(publicationDate__range=[startdate, enddate])
if (descriptionText != None):
users = users.filter(offers__functionDescription__icontains=descriptionText.strip())
offers = offers.filter(functionDescription__icontains=descriptionText.strip())
users = users.prefetch_related('offers', Prefetch(queryset=offers))
Any help? Thank you all :)))
You can use to_attr argument of Prefetch object to prefetch additional queryset:
users = users.filter(offers__description__icontains=sometext).prefetch_related(
Prefetch('offers', queryset=Offer.objects.filter(
publicationDate__range=[startdate, enddate]), to_attr='date_offers'),
Prefetch('offers', queryset=Offer.objects.filter(description__icontains=sometext), to_attr='description_offers')).distinct()
UPD
If you need dynamically add filters to prefetched queryset you can define it separately like this:
if some_case:
users = users.filter(offers__description__icontains=sometext)
offers=Offer.objects.filter(description__icontains=sometext)
if some_case_2:
users = users.filter(**conditions)
offers = offers.filter(**conditions)
users = users.prefetch_related(Prefetch('offers', queryset=offers))
Now each user in users queryset will have two attributes: user.date_offers and user.description_offers.

Django- filtering of the filter object

I want to make a complex filtering on the page using the FilterSets. This is my Filterset, nicely showing me tuples from chosen time and with chosen parameters.
# filters.py
class workFilter(filters.FilterSet):
start__gt = filters.DateTimeFilter(name='time_start', lookup_expr='gte')
start__lt = filters.DateTimeFilter(name='time_end', lookup_expr='lte')
class Meta:
model = Work
fields = ('machine', 'program')
But I want to add charts explaining the queried data. For that I need informations, like overall count of time. I am querying them like that:
#views.py
def search(request):
work_list = Work.objects.all()
work_filter = workFilter(request.GET, queryset=work_list)
filter_backends = (filters.DjangoFilterBackend,)
#some queries to add to context, such as
sum_work = Work.objects.aggregate(Sum('time'))['time__sum']
return render_to_response(
TEMPLATE_DIRS + 'index.html',
{
'filter': praca_filter,
'sum_work': sum_work,
}
)
But sadly, those queries are according to whole database, not to my filtered set of object.
How can I make queries on filtered set work_filter?
Define sum_work as a property of your FilterSet.
class WorkFilter(filters.FilterSet):
start__gt = filters.DateTimeFilter(name='time_start', lookup_expr='gte')
start__lt = filters.DateTimeFilter(name='time_end', lookup_expr='lte')
class Meta:
model = Work
fields = ('machine', 'program')
#property
def work_sum(self):
qs = super(WorkFilter, self).qs
return qs.aggregate(Sum('time'))['time__sum']
Then when you pass your filter through to your view you just need {{ filter.work_sum }} in your template.