Skip to content

Instantly share code, notes, and snippets.

DATABASES = {
'default': {
'ENGINE':'django.db.backends.mysql',
...
'OPTIONS': {'charset': 'utf8mb4'},
}
}
class CreditCardForm(ModelForm):
card_number = CreditCardField(placeholder=u'0000 0000 0000 0000', min_length=12, max_length=19)
from django import forms
from django.forms.widgets import TextInput
from django.utils.translation import ugettext_lazy as _
class TelephoneInput(TextInput):
# switch input type to type tel so that the numeric keyboard shows on mobile devices
input_type = 'tel'
find . \( -name '*.py' -o -name '*.coffee' -o -name '*.scss' \) | xargs wc -l
from django.views.generic import UpdateView
from forms import MyModelForm
from models import MyModel
class MyUpdateView(UpdateView):
# specify a custom ModelForm
form_class = MyModelForm
ids = [9, 8, 1, 2, 7, 3]
results = Model.objects.filter(id__in=ids).extra(
select={'manual': 'FIELD(id,%s)' % ','.join(map(str, ids))},
order_by=['manual']
)
SELECT id, name
FROM table
WHERE name IN (9, 8, 1, 2, 7, 3)
ORDER BY FIELD(id, 9, 8, 1, 2, 7, 3)
from itertools import chain
from operator import attrgetter
# ascending oreder
result_list = sorted(
chain(queryset1, queryset2),
key=attrgetter('date_created'))
# descending order
from itertools import chain
result_list = list(chain(queryset1, queryset2))
import httplib2
from googleapiclient.discovery import build
from googleapiclient.http import HttpError
from oauth2client.client import SignedJwtAssertionCredentials
def get_metrics():
# load the service account key
# this key is managed here - https://console.developers.google.com/project