Aggregate tag names in Django Taggit - python

I am using the django taggit library. I have a Queryset that lists objects, which have a few tags attached to each object. How to get them all in one query?
While I know how to get tags for each objects on model level, I haven't found a way to do so at Queryset level. How to get all objects tags in Queryset in the same query?
Something like
Book.objects.filter(year=2020).values('bookname', 'published_at', 'tag_names_list')[:]

Queryset.values('main_group_field').annotate(all_tags=StringAgg('tags__name', ',')) solves it.

Related

How does django take queryset without database hitting?

There are so many answers and articles to say queryset in django is lazy, it isn't evaluated until you actually do something with queryset.
My question is how is it possible? How does the methods, filter(), all() or order_by() etc, work not knowing what data the objects have?
I assume that hitting a database and knowing data in model objects is different. But, it doesn't make sense for me.
Cheers!
queryset aggragate all filters, excludes annotates and somethins like that and when you do something with this queryset, django generate query (from filters etc. do sql query) to database and after that do qyery do database

Django. E-shop catalogue filters for goods

community!
I've stuck with developing filter system for e-shop on Django.
Some info:
small scale (less than 2-3k items)
django 1.7, postgresql.
1 level category structure. No nested categories
Model skeleton:
Category: - used for, you know, categories
name
AttributeGroup: - used for filter name, ex. "Color"
name
Attribute: - used for filter values to display, ex. "Red", "Green", etc.
name
attribute_group - foreign key to AttributeGroup
Item:
name
attributes - many-to-many to Attribute
category - foreign key to Category.
So, one item can be in only one category. It can have many attributes from the same or different attribute groups. Very close or exact EAV model, but ok because hard caching is enabled.
Actually, my question consist of few:
What will be the best way to work with filters for such a scale?
I'm thinking of actual requests to relational DB tables with ORM filter stuff.
Go noSql way with Postgre docs.
Use solr with it's facets.
How to recreate logic when there is OR logic inside one attribute group but AND logic between different attribute groups?
EDITED
Let's stick to the simplest way - pure Django ORM.
In category view I'm getting category_id and list of activated filters attributes.
so the items queryset will be something like that:
items = Item.objects.filter(category.id = category_id)
Now i need to use filter on this items queryset. attributes list holds ids of all activated attributes. For AND logic it's clear:
for attribute in attributes:
items = items.filter(attribute.id = attribute)
But how can I make OR logic which should be between different groups of attributes?

Order Query in Django Model

Trying to modify a Django project's filter.
I have the following query
queryset = Post.objects.filter(public=True)
I want to modify this to have the query ordered by 'like_count'
How would I implement this?
By using order_by, like this:
q = Post.objects.filter(public=True).order_by('like_count')
If Like is a different table.
The solution in the below link might help.
Django, annotation and ordering using data from other table

Django GenericForeignKey lookup for a given model

I use a voting app (django-ratings if that makes any difference) which uses django's GenericForeignKey, has a ForeignKey to User, and several other fields like date of latest change.
I'd like to get all the objects of one content type, that a single user voted for ordered by date of latest change. As far as I understand - all the info can be found in a single table (except the content_type which can be prefetched/cached). Unfortunately django still makes an extra query each time I request a content_object.
So the question is - how do I get all the votes on a given model, by a given user, with related objects and given ordering with minimum database hits?
Edit: Right now I'm using 2 queries - first selecting all the votes, getting all the objects I need, filtering by .filter(pk__in=obj_ids) and finally populating them to votes objects. But it seems that a reverse generic relation can help solve the problem
Have you checked out select_related()? That may help.
Returns a QuerySet that will automatically "follow" foreign-key relationships, selecting that additional related-object data when it executes its query. This is a performance booster which results in (sometimes much) larger queries but means later use of foreign-key relationships won't require database queries.
https://docs.djangoproject.com/en/dev/ref/models/querysets/#select-related
Well right now we're using prefetch_related() from django 1.4 on a GenericRelation. It still uses 2 queries, but has a very intuitive interface.
From looking at the models.py of the django-ratings app, I think you would have to do user.votes.filter(content_type__model=Model._meta.module_name).order_by("date_changed") (assuming the model you want to filter by is Model) to get all the Vote objects. For the related objects, loop through the queryset getting content_object on each item. IMHO, this would result in the least DB queries.

How to filter fields through DBRef using MongoKit?

I want to filter fields to a document containing DBRef when it's fetched.
The target filtering fields is a child document had relation using DBRef.
I wanna code like this.
db.User.find(fields=['_id', 'profile._id', 'profile.text'])
The 'profile' field is using DBRef for this example.
Does someone have any idea?
So User documents each have a DBRef to a Profile document? Looking at the MongoKit source, it seems to me that what you want isn't possible:
https://github.com/namlook/mongokit/blob/master/mongokit/document.py#L647
See how it does one() (which is effectively a find()) to fetch the referred-to document from MongoDB? That's where MongoKit would have to pass in a list of fields to include or exclude in the referred-to doc, but MongoKit does not. You might open a feature request with the author, or see if you can add it yourself and submit a patch.

Categories