How can I filter a Django query with a list of values?
From the Django documentation: Blog.objects.filter(pk__in=[1, 4, 7])
From the Django documentation: Blog.objects.filter(pk__in=[1, 4, 7])
See the docs FAQ: “How can I see the raw SQL queries Django is running?” django.db.connection.queries contains a list of the SQL queries: from django.db import connection print(connection.queries) Querysets also have a query attribute containing the query to be executed: print(MyModel.objects.filter(name=”my name”).query) Note that the output of the query is not valid SQL, because: “Django … Read more
If it’s a value you’d like to have for every request & template, using a context processor is more appropriate. Here’s how: Make a context_processors.py file in your app directory. Let’s say I want to have the ADMIN_PREFIX_VALUE value in every context: from django.conf import settings # import the settings file def admin_media(request): # return … Read more
Errors are stored in the nginx log file. You can specify it in the root of the nginx configuration file: error_log /var/log/nginx/nginx_error.log warn; On Mac OS X with Homebrew, the log file was found by default at the following location: /usr/local/var/log/nginx
Reserved.objects.filter(client=client_id).order_by(‘-check_in’) Notice the – before check_in. Django Documentation
This is a part of security, you cannot do that. If you want to allow credentials then your Access-Control-Allow-Origin must not use *. You will have to specify the exact protocol + domain + port. For reference see these questions : Access-Control-Allow-Origin wildcard subdomains, ports and protocols Cross Origin Resource Sharing with Credentials Besides * … Read more
There are a couple of ways: To delete it directly: SomeModel.objects.filter(id=id).delete() To delete it from an instance: instance = SomeModel.objects.get(id=id) instance.delete()
UPDATE With Python3, you can do it in one line, using SimpleNamespace and object_hook: import json from types import SimpleNamespace data=”{“name”: “John Smith”, “hometown”: {“name”: “New York”, “id”: 123}}” # Parse JSON into an object with attributes corresponding to dict keys. x = json.loads(data, object_hook=lambda d: SimpleNamespace(**d)) print(x.name, x.hometown.name, x.hometown.id) OLD ANSWER (Python2) In Python2, … Read more
Your understanding is mostly correct. You use select_related when the object that you’re going to be selecting is a single object, so OneToOneField or a ForeignKey. You use prefetch_related when you’re going to get a “set” of things, so ManyToManyFields as you stated or reverse ForeignKeys. Just to clarify what I mean by “reverse ForeignKeys” … Read more
There is Q objects that allow to complex lookups. Example: from django.db.models import Q Item.objects.filter(Q(creator=owner) | Q(moderated=False))