In my fixturesquery below you can see I am filtering by the results of the teamsquery, and it works, but only for the first result of the teamsquery. So it only outputs the fixtures for the first userteam__userID=request.user
teamsquery = Team.objects.filter(userteams__userID=request.user)
fixturesquery = Fixtures.objects.filter(Q(hometeamID=teamsquery) |
Q(awayteamID=teamsquery))
How do i fix it so it outputs the fixtures for all the results of teamsquery?
If I understand correctly, your user can have multiplte teams, right?
If so, you can use:
teamsquery = Team.objects.filter(userteams__userID=request.user)
fixturesquery = Fixtures.objects.filter(Q(hometeamID__in=teamsquery)|Q(awayteamID__in=teamsquery))
Related
Is there a way in Django to achieve the following in one DB hit (Debug Toolbar shows 2 queries)?
q = SomeModel.objects.filter(name=name).order_by(some_field)
if q.count() == 0:
q = SomeModel.objects.all().order_by(some_field)
I want to check if there are objects with a given name. If yes, then return them. If not, return all objects. All done in one query.
I've checked Subquery, Q, conditional expressions but still don't see how to fit it into one query.
Ok, much as I resisted (I still think it's premature optimization), curiosity got the better of me. This is not pretty but does the trick:
from django.db.models import Q, Exists
name_qset = SomeObject.objects.filter(name=name)
q_func = Q(name_exists=True, name=name) | Q(name_exists=False)
q = SomeModel.objects.annotate(
name_exists=Exists(name_qset)
).filter(q_func).order_by(some_field)
Tried it out and definitely only one query. Interesting to see if it is actually appreciably faster for large datasets...
You best bet is to use .exists(), otherwise your code is fine
q = SomeModel.objects.filter(name=name).order_by(some_field)
if not q.exists():
q = SomeModel.objects.all().order_by(some_field)
I have for example the following lists:
list1 = ['blue', 'red']
list2 = ['green', 'yellow', 'black']
How could i create a query searching for all values (using postgresql).
This will work fine but it's hardcoded for only two values and therefore not handling the varying amount of values of the lists.
entry.objects.annotate(search=SearchVector('colors')).filter(search=SearchQuery('blue') | SearchQuery('red'))
I tried the following to create a 'search_query' string and place it in my query:
for c in color:
search_string += "SearchQuery('" + c +"') | "
search_string = search_string[:-3]
This will result in the following search_strings
SearchQuery('blue') | SearchQuery('red')
SearchQuery('green') | SearchQuery('yellow') | SearchQuery('black')
If i now put this string in my query, it will not work.
entry.objects.annotate(search=SearchVector('colors')).filter(search=search_string)
I appreciate any help to solve this problem.
Link to django postgres search documentation: https://docs.djangoproject.com/en/1.11/ref/contrib/postgres/search/
I don't know about postgreSQL but:
# Can run this query with SQL (i think django query auto convert for each type of DataBase???)
res = Models.Objects.filter(color__in = list1)
more_res = Models.Objects.filter( Q(color__in = list1) | Q(color__in = list2))
Django query basic (i don't know if it work for you???)
Your search_string is not working because it is, well, a string.
You can use eval to make it work. Try this:
entry.objects.annotate(search=SearchVector('colors')).filter(search=eval(search_string))
It is not the best way though. Take a look at this
Does someone can explain what is going on with my sql statement. Here is my code chunk.
for so in self.pool.get('sale.order').browse(cr,uid,so_id,context):
_logger.info("\n\n\t\t\t SALE ORDER ID %d"%(so.id))
confirmed_by = so.confirmed_by.id
_logger.info("\n\n\t\t\tconfirmed by %s"%(str(confirmed_by)))
rg_id = cr.execute("select rg.id from res_users ru,res_groups rg,res_groups_users_rel rgr
where ru.id = rgr.uid and rgr.gid = rg.id and ru.id = "+str(confirmed_by)+" and rg.name like 'Project Second User'")
_logger.info("\n\n\t\t\tRES GROUPS IDS %s"%(rg_id))
My confirmed by returns an id but I don't know why rg_id returns None when executed. When used in PgAdmin my query works fine.
Any help is very much appreciated.
This is how manage to solve the None issue:
cr.execute("select rg.id from res_users ru,res_groups rg,res_groups_users_rel rgr
where ru.id = rgr.uid and rgr.gid = rg.id and ru.id = "+str(confirmed_by)+" and rg.name like 'Project Second User'")
rg_id = cr.fetchall()
Now, my rg_id is returning either an id from a table res_groups or [] if there is no record found.
I'm here with a problem that does not quite know how to solve.
I have this query:
result = json.dumps([a.get_json() for a in Player.objects.filter(name=namepost)])
But now I want to return the result with paging, and do not really know how to do ... I've been seeing in the documentation to use the Paginator.
But for example when I do this
p = Paginator(result, 2)
print p.count
Gives 1609 ... and the result of the query is 3 records.
Someone can help me?
Doing a search using django-sphinx gives me results._sphinx that says there were 68 results, but when I iterate over them, I can only get at the first 20 of them.
I'm SURE there's a way around this, and that this is by design, but it's officially stumping the heck out of me. Does anybody know how to get the complete queryset?
I figured this out finally.
Apparently, the querysets only return 20 hits until you access the queryset. Or something like that.
So, if you explicitly want the iterate over the whole thing, you have to do:
for result in results[0:results.count()]:
print result
Or something to that effect, which will query the entire thing explicitly. Ugh. This should be clearly documented...but it not.
After hacking through source, I set the _limit variable explicitly.. Does the job, and issues an actual limit:
qs = MyEntity.search.query(query_string)
qs._limit = limit
for result in qs:
print result
work for me:
in sphinx config file:
max_matches = 5000
in django code:
desc_obj = Dictionary.search.query( search_desc )
desc_obj._maxmatches = 5000
or in settings:
SPHINX_MAX_MATCHES = 5000