I'm trying to order_by a query but i cannot seem to accomplish it, any help would be appreciated
users = User.objects.filter(Q(groups__name=group)).distinct()
This is the starting query i have tried many ways to make this work with the order_by method. But cant seem to get it working i am trying to order the query by the first_name in descending order.
.order_by('-first_name'.desc())
Something like this?
I get an error 'str' object has no attribute 'desc'
I have tried to look this up but cant see it being produced in the context that i am using it so i cant relate to the answers
-- Edit --
i have had some progress, Names are changing places but i cant seem to match a pattern, So i am thinking that (Q(groups__name=group)) could be the cause of it but i have no idea what this actually does, Can anyone explain this to me? thanks
use
.order_by('-first_name')
instead of
.order_by('-first_name'.desc())
- stands for descending already. otherwise you are trying to call a method on a string instead of QuerySet object
Related
Im looking for a solution for DRF where I would get query like this:
..../?sort=foo&order=asc/desc
I was able to change order param to "sort" but I dont know how to add explicitly "&order=asc" except current solution which uses "-" before column.
thank you in advance.
I have three class in my ontology
class babyAge (6+, 8+, 12+)
class taste (asam, manis, asin)
class food (recipe name)
i want to querying data food for babyAge (6+) and have taste (asam)
i use this code in my java program (i dont know if there are other code more simple than this one, if you know please tell me too) :
SELECT ?babyAge ?taste ?food
WHERE { ?babyAge mpasi:hasResep ?food.
?food mpasi:hasRasa ?taste.
FILTER regex(str(?babyAge),"6+").
FILTER regex (str(?taste),"asam")}
and i get my data. but when i execute that code in protege, i dont get anything. Can someone help me?
It's difficult to tell without seeing the ontology.
My first move would be to ensure mpasi is bound to the same value in the ontology and in your query - it might be a case of mismatched IRIs.
You might also want to try without filters and see if anything is returned.
I have a search index that I have created using Solr. I want to add individual django objects to the search index.
To remove objects from the solr database we use remove_object.
some = SomFooModel.objects.get(pk=1)
foo = FooIndex()
foo.remove_object(some) #This works
To add it, is there something like add_object or a work around here ?
What I want is.
foo.add_object(some). # there is no such thing
This also does not work. It does not add the object to index.
foo.update_object(some)
I have tried reading the django-haystack documentation but there seems to be nothing that might help.
I did not read the documentation well enough as a result I messed up on the QuerySet part.
foo.update_object(some)
The above does add the object to the index. Its just that I was not searching for it properly.
I was searching for the object after removing it in the following way.
SearchQuerySet().filter(foo=some.foo)
This gave a empty query set always.
SearchQuerySet().models(SomFooModel).filter(foo = some.foo)
This gives the correct result.
Reference
I'm trying to set up my first CRUD page using django and so far I've got to the D part, respectivly - delete. Following the information found on the internet, I tried this: SomeModel.objects.filter(id=id).delete(), but that threw out an error: id() takes exactly one argument (0 given) so could you tell me what am I doing wrong in order to make it work please?
Thank you.
SomeModel.objects.filter(id=id)[0].delete()
I am almost done with my first production-ready django-project. I got one big problem left:
I got an article-search-view that renders a list of found articles. Pagination is working just fine for the resultlist. When I click on the article-title the object-detail-page opens. What I want: previous- and next-result-links on the object-detail-page.
I tried several approaches to similar problems but didn't find a working solution. If I try to use a paginator with only one article (for the object-detail-page) I need to know that paginator-index in the resultlist. But how?
Even the .get_next(previous)_by_foo-Method is not really usable in this scenario AFAICT. Or am I missing something obvious here? Thanks for any help in advance!
Paginator from django works with lists. A way of searching indexes in lists it's like that:
['aaa', 'bbb', 'ccc'].index('bbb') # result: 1
or so like this:
model = object()
[object(), object(), model].index(model) # result: 2
Hope that gives you a hint on how you find paginator-index on your list.
If you want to get a link, at object-details-page, to next item from search-result, you must get next item from the search-result. To get a next item you need to perform the same search query which was executed in search-page and apply some extra filters to get only the next item from that list. But here you have a problem: you only have object-id in object-details page, you don't have the search-term. Without search-term you won't be able to create the search-query. That means you need to get search-term somehow. How do you get that search-term? You need to pass it from the search-result-page somehow. You can save the search-term in session/cookie, or, maybe better: you can pass it via a GET parameter to object-details page. Now when you search-term in object-display page, you can perform a search-query, and from that query you can select the next and the previous objects.
I think now you should be able to implement that. If not you could show some of your code of object-details view, maybe someone will write some code for you.
you could use this
next »
« previous