Django conditional count - mysql

I am querying the names of all the Tags that I've flagged as "visible":
visible_tags = Tag.objects.filter(visible=True,taggit_taggeditem_items__content_type=ContentType.objects.get_for_model(Action)).order_by('name')
I want to add a field called "action_count" that tells me how many actions are associated with this tag:
visible_tags = Tag.objects.filter(visible=True,taggit_taggeditem_items__content_type=ContentType.objects.get_for_model(Action)).order_by('name').annotate(action_count=Count('action'))
This works except that I want to know not just now many actions are affiliated, but how many actions that are incomplete that are affiliated with this tag.
I tried the following:
visible_tags = Tag.objects.filter(visible=True,taggit_taggeditem_items__content_type=ContentType.objects.get_for_model(Action)).order_by('name').filter(action__complete=False).annotate(action_count=Count('action'))
But this doesn't quite do what I need it to do. How can I annotate the count of actions that are incomplete?

You might get more count than expected. This is because chained .filter that introduce extra inner join, very similar to the question Are chained QuerySet filters equivalent to defining multiple fields in a single filter with the Django ORM?
Thus put the second filter in the first one:
visible_tags = Tag.objects.filter(visible=True, taggit_taggeditem_items__content_type=ContentType.objects.get_for_model(Action),
action__complete=False # Here
).order_by('name').annotate(action_count=Count('action'))
Besides, print queryset.query to know what SQL Django generates for you.

Related

How to limit results in reverse relation in Django

I have two tables, one called Company and the other called User, each user is related to one company using ForeignKey. So I can use reverse relation in Django to get all users for specific company (e.g. company.users)
In my case, I'm building ListAPIView which return multiple companies, and I'd like to return latest created user. My problem is that I don't want to use prefetch_related or select_related so it will load all the users, as we might end up having thousands of users per each company! Also I don't want to load each latest user in a separate query so we end up having tens of queries per API request!
I've tried something like this:
users_qs = models.User.objects.filter(active=True).order_by('-created')
company_qs = models.Company.objects.prefetch_related(
Prefetch('users', queryset=users_qs[:1], to_attr='user')
).order_by('-created')
In this case, prefetch_related failed as we can't set limit on the Prefetch's queryset filter (it gives this error "Cannot filter a query once a slice has been taken.")
Any ideas?
I think you are providing an object instead of a queryset Prefetch('users', queryset=users_qs[:1], to_attr='user')

correctly fetch nested list in SQL

I have a design problem with SQL request:
I need to return data looking like:
listChannels:
-idChannel
name
listItems:
-data
-data
-idChannel
name
listItems:
-data
-data
The solution I have now is to send a first request:
*"SELECT * FROM Channel WHERE idUser = ..."*
and then in the loop fetching the result, I send for each raw another request to feel the nested list:
"SELECT data FROM Item WHERE idChannel = ..."
It's going to kill the app and obviously not the way to go.
I know how to use the join keyword, but it's not exactly what I want as it would return a row for each data of each listChannels with all the information of the channels.
How to solve this common problem in a clean and efficient way ?
The "SQL" way of doing this produces of table with columns idchannel, channelname, and the columns for item.
select c.idchannel, c.channelname, i.data
from channel c join
item i
on c.idchannel = i.idchannel
order by c.idchannel, i.item;
Remember that a SQL query returns a result set in the form of a table. That means that all the rows have the same columns. If you want a list of columns, then you can do an aggregation and put the items in a list:
select c.idchannel, c.channelname, group_concat(i.data) as items
from channel c join
item i
on c.idchannel = i.idchannel
group by c.idchannel, c.channelname;
The above uses MySQL syntax, but most databases support similar functionality.
SQL is made for accessing two-dimensional data tables. (There are more possibilities, but they are very complex and maybe not standardized)
So the best way to solve your problem is to use multiple requests. Please also consider using transactions, if possible.

How do I separate the results

I have the following query on Doctrine QueryBuilder:
$qb = $this->createQueryBuilder('e')
->select('e.id, e.name, e.body, e.teaser, e.slug, e.dateBegin, e.dateEnd, e.dateTbd, v.name AS v_name')
->innerJoin('e.venue', 'v')
->where('v.name LIKE :TBD')
->orWhere('v.name LIKE :TBA')
->orWhere('e.name LIKE :TBD')
->orWhere('e.name LIKE :TBA')
->orWhere('e.name LIKE \'none\'')
->orWhere('e.name LIKE \'n/a\'')
->orWhere('e.teaser LIKE :TBD')
->orWhere('e.body LIKE :TBD')
->orWhere('e.dateTbd=true')
->orWhere('TIME(e.dateBegin) < :earlyMorning AND TIME(e.dateBegin) > :lateNight')
->setParameter('TBA', '%TBA%')
->setParameter('TBD', '%TBD%')
->setParameter('earlyMorning', '06:00:00')
->setParameter('lateNight', '23:00:00');
How I can separate the results by 'where' clause in this query. I need to display every event with criteria which listed in where clause.
I can't see a way of achieving this easily, especially if we are to assume you're using this to retrieve a standard doctrine entity collection, since additional grouping / properties wouldn't really be something you could pass into these.
If you need to group these simply to display them slightly differently as they're output, say, in your markup, I'd consider adding additional methods into your entity's class to allow you detect which 'type' they are based on similar criteria defined in your query, e.g. using standard string matching functions.
Otherwise your options are going to be filtering the collection (ArrayCollections carry a method for doing that easily) or separating it out into multiple queries, which given your approach, I assume you are trying to avoid.

Can I create sperate queries for different views?

I'm learning sqlalchemy and not sure if I grasp it fully yet(I'm more used to writing queries by hand but I like the idea of abstracting the queries and getting objects). I'm going through the tutorial and trying to apply it to my code and ran into this part when defining a model:
def __repr__(self):
return "<User('%s','%s', '%s')>" % (self.name, self.fullname, self.password)
Its useful because I can just search for a username and get only the info about the user that I want but is there a way to either have multiple of these type of views that I can call? or am I using it wrong and should be writing a specific query for getting different data for different views?
Some context to why I'm asking my site has different templates, and most pages will just need the usersname, first/last name but some pages will require things like twitter or Facebook urls(also fields in the model).
First of all, __repr__ is not a view, so if you have a simple model User with defined columns, and you query for a User, all the columns will get loaded from the database, and not only those used in __repr__.
Lets take model Book (from the example refered to later) as a basis:
class Book(Base):
book_id = Column(Integer, primary_key=True)
title = Column(String(200), nullable=False)
summary = Column(String(2000))
excerpt = Column(Text)
photo = Column(Binary)
The first option to skip loading some columns is to use Deferred Column Loading:
class Book(Base):
# ...
excerpt = deferred(Column(Text))
photo = deferred(Column(Binary))
In this case when you execute query session.query(Book).get(1), the photo and excerpt columns will not be loaded until accessed from the code, at which point another query against the database will be executed to load the missing data.
But if you know before you query for the Book that you need the column photo immediately, you can still override the deferred behavior with undefer option: query = session.query(Book).options(undefer('photo')).get(1).
Basically, the suggestion here is to defer all the columns (in your case: except username, password etc) and in each use case (view) override with undefer those you know you need for that particular view. Please also see the group parameter of deferred, so that you can group the attributes by use case (view).
Another way would be to query only some columns, but in this case you are getting the tuple instance instead of the model instance (in your case User), so it is potentially OK for form filling, but not so good for model validation: session.query(Book.id, Book.title).all()

nested sql queries in rails

I have the following query
#initial_matches = Listing.find_by_sql(["SELECT * FROM listings WHERE industry = ?", current_user.industry])
Is there a way I can run another SQL query on the selection from the above query using a each do? I want to run geokit calculations to eliminate certain listings that are outside of a specified distance...
Your question is slightly confusing. Do you want to use each..do (ruby) to do the filtering. Or do you want to use a sql query. Here is how you can let the ruby process do the filtering
refined list = #initial_matches.map { |listing|
listing.out_of_bounds? ? nil : listing
}.comact
If you wanted to use sql you could simply add additional sql (maybe a sub-select) it into your Listing.find_by_sql call.
If you want to do as you say in your comment.
WHERE location1.distance_from(location2, :units=>:miles)
You are mixing ruby (location1.distance_from(location2, :units=>:miles)) and sql (WHERE X > 50). This is difficult, but not impossible.
However, if you have to do the distance calculation in ruby already, why not do the filtering there as well. So in the spirit of my first example.
listing2 = some_location_to_filter_by
#refined_list = #initial_matches.map { |listing|
listing.distance_from(listing2) > 50 ? nil : listing
}.compact
This will iterate over all listings, keeping only those that are further than 50 from some predetermined listing.
EDIT: If this logic is done in the controller you need to assign to #refined_list instead of refined_list since only controller instance variables (as opposed to local ones) are accessible to the view.
In short, no. This is because after the initial query, you are not left with a relational table or view, you are left with an array of activerecord objects. So any processing to be done after the initial query has to be in the format of ruby and activerecord, not sql.