One of the benefits of
One of the ways that executes the query is using iteration:
In the below ways your query will be evaluated (executed):
- repr
- len
- list (e.g.: `list(Entry.objects.all())`)
- bool
#python #django #querySet #query_set #evaluation
Django`'s `QuerySet
is that you can apply filters on it, slice it and move it around your classes without actually hitting the database until you do so.One of the ways that executes the query is using iteration:
for c in Course.objects.all():
print(c.title)
NOTE:
the first time you iterate over the query, it hits the database and get the result NOT in each iteration.In the below ways your query will be evaluated (executed):
- repr
- len
- list (e.g.: `list(Entry.objects.all())`)
- bool
#python #django #querySet #query_set #evaluation
DJANGO
Let's do a little bit more by fetching data from database and pass them to views in
Django
.For now we make a change in
polls/views.py
in index
function to get data from Question
model and return it as an HTTP response object. At first let's import the model:from .models import Question
And now in
index
function write the below code:latest_question_list = Question.objects.order_by('-pub_date')[:5]
output = ', '.join([q.question_text for q in latest_question_list])
return HttpResponse(output)
The code above gets the last 5 questions and on the second line
question_text
fields for those questions are joined together by comma and finally returned as an HTTP response.
Now if you head over to
http://127.0.0.1:8000/polls/
you would see the result of question texts bound together by comma.
#python #django #query #django_part9
How to kill a process in
If you are unfamiliar with your current database schema and indexes, or run a heavy query by mistake on production database things go nasty! To find the query and kill it run mongo client as below and run
to run
#mongodb #mongo #killOp #currentOp #query
mongo
shell?If you are unfamiliar with your current database schema and indexes, or run a heavy query by mistake on production database things go nasty! To find the query and kill it run mongo client as below and run
currentOp
command:> mongo
> db.currentOp()
currentOp()
database method will display the ongoing queries in json format, find the query which is related to you and get its opid
. Now you needto run
killOp()
database method to kill that process as below:db.killOp(229)
229
is the operation id we have got from the first command.NOTE:
terminate running operations with extreme caution. Only use db.killOp() to terminate operations initiated by clients and do not terminate internal database operations.#mongodb #mongo #killOp #currentOp #query