django q objects
Poll.objects.get(
Q(question__startswith='Who'),
Q(pub_date=date(2005, 5, 2)) | Q(pub_date=date(2005, 5, 6))
)
Related cards:
django query managers
managers
# First, define the Manager subclass.
class DahlBookManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(author='Roald Dahl')
# Then hook it into the Book model explicitly.
class Book(mod...
django admin action
Sample duplicate event
This example makes use of clearing the id field (primary key) and then saves the record to produce a duplicate of the record. The user can select as many records from the admin panel and it will duplicate each in the sel...
django management commands
Management Commands
Add a management/commands directory to the application. Django will register a manage.py command for each Python module in that directory whose name doesn’t begin with an underscore.
Example
from django.core.manageme...
django groups
Every field you add to the values() clause, will be used to create the group by query
City.objects.values('name', 'country__name').annotate(Sum('population'))
django CSRF Disable
Sample class for CSRF disable
from rest_framework.authentication import SessionAuthentication, BasicAuthentication
class CsrfExemptSessionAuthentication(SessionAuthentication):
def enforce_csrf(self, request):
return # To not p...
django tables2 installation
Django-tables2 is Available on pypi and can be installed using pip:
pip install django-tables2
After installing, add 'django_tables2' to INSTALLED_APPS and make sure that 'django.template.context_processors.request' is added to the context_proces...
django DRF Permissions
Settings
global settings for a REST framework API are kept in a single configuration dictionary named REST_FRAMEWORK.
REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for una...
django bulk insert
bulk_create
import utils
from django.utils import timezone
from app import models
def orm_bulk_create(n_records):
instances = [
models.TestModel(
field_1=i,
field_2=str(i),
field_3=timezon...
django apps in subfolder
sample manage.py
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
# Add the apps directoriy to Python's path. In production it will
# be n...