django

django import config

from django.conf import settings

if settings.DEBUG:
    # Do something

django send mail

In two lines:

from django.core.mail import send_mail

send_mail(
    'Subject here',
    'Here is the message.',
    'from@example.com',
    ['to@example.com'],
    fail_silently=False,
)
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'                    
EMAIL_HOST = 'smtp.exmail.qq.com'                                                   
EMAIL_HOST_USER = 'sys@yourdomain.com'                                             
EMAIL_HOST_PASSWORD = 'youpass'                                              
EMAIL_TIMEOUT = 10

django field lookup

isnull Takes either True or False, which correspond to SQL queries of IS NULL and IS NOT NULL, respectively.

Entry.objects.filter(pub_date__isnull=True)
SELECT ... WHERE pub_date IS NULL;

django annotate

from django.db.models import Sum

Title.objects.values('publisher').annotate(tot_dbl_prices=2*Sum('price'))
MyModel.objects.all().annotate(mycolumn=Value('xxx', output_field=CharField()))

django change field name in queryset

MyModel.objects.extra(select={'renamed_value': 'cryptic_value_name'}).values('renamed_value')

deploy django project

gunicorn

–capture-output: let gunicorn capture django’s standard output, eg. log info

gunicorn myproject.wsgi:application -b 127.0.0.1:18100 -t 180 -w 1 --capture-output --log-level INFO -p /var/myproject/run/myproject.pid