I've read the documentation on how to do this but it is not working. I am trying to get my query to sort by date newest first, but this is not working:
wrQuery = WorkRequest.query()
wrQuery.order('-date')
wrResult = wrQuery.fetch()
I have also tried ('-WorkRequest.date') but both results in similar errors:
TypeError: order() expects a Property or query Order; received '-date'
The query objects are immutable, your 3rd statement references the object from your 1st statement, not the ordered one created in the 2nd statement. See also NDB Query builder doesn't work as expected.
This should work instead:
wrQuery = WorkRequest.query()
wrOrderedQuery = wrQuery.order('-WorkRequest.date')
wrResult = wrOrderedQuery.fetch()
This looks like you are using NDB rather than DataStore, in which case the docs show you unequivocally how to do it. This should work:
wrQuery = wrQuery.order(-WorkRequest.date)
Related
I think I have misunderstood something critical and obvious, I am attempting to use the SqlAlchemy ORM to select a row from a table in a database and return the values. The first step is to get the row and access it, from there I think I'm good however, when I select the row and try and investigate the object that is returned, all I find is an address for the object in memory: <main.UserMap object at 0x000001F65A54B490>,
Attempting to use objprint to investigate the object gives no further information, I am confused as per my understanding the resulting row object should behave like a tuple so at least objprint should find a variety of entries within it even if it can't print them due to them being differen't data types.
The statement in question:
select(UserMap).where(UserMap.google_auth == '***********************')
a more basic select(UserMap) also seems to give a similar result.
The table contains some fields as strings, some as integers and some in date and time formats but obviously only one type per column.
I am using session.execute, I would like to avoid query as I understand that it's functionality is being deprecated in the 2.x API, if I have misunderstood this then I am happy to attempt that but would still like to understand what I am getting wrong about the result and row objects.
As I said, I think I have missed something important, but don't realise what, as far as I can see I am executing the statements in much the same way as shown in the ORM documentation e.g:
>>> stmt = select(User).where(User.name == 'spongebob')
>>> with Session(engine) as session:
... for row in session.execute(stmt):
... print(row)
<main.UserMap object at 0x000001F65A54B490> is the default string representation of an ORM object. You can retrieve the attributes (properties) of the object using the standard syntax:
me = UserMap(user_name="Gord")
print(me) # <main.UserMap object at 0x000001F65A54B490>
print(me.user_name) # Gord
I was trying to query a database based on some pre selected items and ran into a weird situation. I started with pre selecting some parameters that I would like use as filter in a query from one of the tables in the database:
MX_noaa_numbers = list(Events_df[Events_df['flareclass'].str.contains('M|X')].noaanumber.unique())
Which produces a list such as:
[11583,11611,11771,11777,11778,11865,12253,11967,11968,...,12673]
But when I tried to obtain the results using:
session.query(ActiveRegion).filter(sql.or_(ActiveRegion.noaa_number1.in_(MX_noaa_numbers),
ActiveRegion.noaa_number2.in_(MX_noaa_numbers),
ActiveRegion.noaa_number3.in_(MX_noaa_numbers))).all()
it returns me an empty list. However if I print MX_noaa_numbers and copy the output inside the in_() statement substituting the object name (MX_noaa_numbers) I actually get the results as I should. Am I missing something or I actually ran into some weird error?
Thanks!
I have a table that stores tasks submitted by users, with timestamps. I would like to write a query that returns certain rows based on when they were submitted (was it this day/week/month..).
To check if it was submitted on this week, I wanted to use date.isocalendar()[1] function. The problem is, that my timestamps are datetimes, so I would need to transform those to dates.
Using func:
filter(func.date(Task.timestamp) == datetime.date(datetime.utcnow()))
works properly.
But I need the date object's isocalendar() method, so I try
filter(func.date(Task.timestamp).isocalendar()[1]==datetime.date(datetime.utcnow()).isocalendar()[1])
and it's no good, I get AttributeError: Neither 'Function' object nor 'Comparator' object has an attribute 'isocalendar'
If I make a simple query and try datetime.date(task.timestamp).isocalendar()[1] it works properly.
How do I get it to work in the query's filter?
Rule of thumb when understanding and debugging sqlalchemy queries is to always think – "How will it look in SQL?"
isocalendar() is a python function, and sqlalchemy query filters get compiled to SQL. Moreover, isocalendar() returns a tuple – and while rendering tuple comparison as SQL is probably possible, it's more trouble then it's worth. You should compare scalars and find sql date functions that suit you.
It seems you're looking to compare week number, so something like this should do the trick:
filter(func.week(Task.timestamp)==datetime.utcnow().isocalendar()[1])
Can you try sqlalchemy.extract(func.date('year', Task.timestamp)) == ... ?
You cannot mix pure python functions with those which are executed on the SQL backend. From your code it looks like you are trying to filter on the iso week. One way to do it would be to load everything from the database into memory and perform the filtering there. Obviously, it most cases it is far from efficient.
An alternative would be to use respective SQL functions, which sqlalchemy will call for you. On MySQL it looks like the function you need is weekofyear, so your filter might look similar to below:
_utcnow = datetime.utcnow().date()
_isoweek = _utcnow.isocalendar()[1]
q = db.session.query(...)
# ...
q = q.filter(db.func.weekofyear(Task.timestamp) == _isoweek)
Consider this query:
query = Novel.objects.< ...some filtering... >.annotate(
latest_chapter_id=Max("volume__chapter__id")
)
Actually what I need is to annotate each Novel with its latest Chapter object, so after this query, I have to execute another query to select actual objects by annotated IDs. IMO this is ugly. Is there a way to combine them into a single query?
Yes, it's possible.
To get a queryset containing all Chapters which are the last in their Novels, simply do:
from django.db.models.expressions import F
from django.db.models.aggregates import Max
Chapters.objects.annotate(last_chapter_pk=Max('novel__chapter__pk')
).filter(pk=F('last_chapter_pk'))
Tested on Django 1.7.
Possible with Django 3.2+
Make use of django.db.models.functions.JSONObject (added in Django 3.2) to combine multiple fields (in this example, I'm fetching the latest object, however it is possible to fetch any arbitrary object provided that you can get LIMIT 1) to yield your object):
MainModel.objects.annotate(
last_object=RelatedModel.objects.filter(mainmodel=OuterRef("pk"))
.order_by("-date_created")
.values(
data=JSONObject(
id="id", body="body", date_created="date_created"
)
)[:1]
)
Yes, using Subqueries, docs: https://docs.djangoproject.com/en/3.0/ref/models/expressions/#subquery-expressions
latest_chapters = Chapter.objects.filter(novel = OuterRef("pk"))\
.order_by("chapter_order")
novels_with_chapter = Novel.objects.annotate(
latest_chapter = Subquery(latest_chapters.values("chapter")[:1]))
Tested on Django 3.0
The subquery creates a select statement inside the select statement for the novels, then adds this as an annotation. This means you only hit the database once.
I also prefer this to Rune's answer as it actually annotates a Novel object.
Hope this helps, anyone who came looking like much later like I did.
No, it's not possible to combine them into a single query.
You can read the following blog post to find two workarounds.
I am trying to sort a collection called user_score using the key position and get the very first document of the result. In this case the collection user_score doesn't exist and I was hoping to get the result as None, but i was getting a cursor back.
1.
result =
db.user_score.find({'score':'$lt':score}}).sort("position,pymongo.DESCENDING").limit(1)
Now i changed my query like below and did not get anything as expected.
2.
result =
db.user_score.find_one({'score':{'$lt':score}}, sort=[("position", pymongo.DESCENDING)])
What's the problem with my first query?
Thanks
A little late in my response but it appears that the current version of PyMongo does support a sort operation on a find_one call.
From the documentation page here:
All arguments to find() are also valid arguments for find_one(),
although any limit argument will be ignored. Returns a single
document, or None if no matching document is found.
Example usage is as follows:
filterdict = {'email' : 'this.is#me.com'}
collection.find_one(filterdict, sort=[('lastseen', 1)])
Hope this helps more recent searchers!
In your first query, in the sort function you're passing one argument ("position,pymongo.DESCENDING"), when you should be passing two arguments ("position", pymongo.DESCENDING).
Be sure to mind your quotation marks.
This is the default mongodb behavior on find. Whenever you use find you get a list of the result (in this case an iterable cursor). Only findOne - or it's PyMongo equivalent find_one will return None if the query has no matches.
Use list to convert the value of the cursor into a dict:
list(db.user_score.find({'score':'$lt':score}}).sort("position",pymongo.DESCENDING).limit(1))[0]