You are on page 1of 1

#Django Cheat Sheet and Quick Reference

#Database Engines #Example Model


from django.db import models #Useful Links
django.db.backends.postgresql
from django.utils import timezone
Django Website - https://www.djangoproject.com
django.db.backends.mysql
class Make(models.Model): Pretty Printed Website - http://prettyprinted.com
django.db.backends.sqlite3
name = models.CharField(max_length=15)
Pretty Pretty YouTube Channel -
django.db.backends.oracle factory_address = models.TextField() https://www.youtube.com/c/prettyprintedtutorials

#Example configuration in settings.py email = models.EmailField()

DATABASES = { def __str__(self):


'default': { return self.name

'ENGINE': 'django.db.backends.mysql',
class Car(models.Model):
'NAME': 'databasename', name = models.CharField(max_length=20)
active = BooleanField()
'USER': 'username',
date_added = DateTimeField(default=timezone.now)
'PASSWORD': 'password', price = FloatField()
make = models.ForeignKey(Make)
'HOST': '127.0.0.1',

'PORT': '5432', def __str__(self):

} return self.name

}
#Create Objects
#Model Fields
from .models import Make, Car
BooleanField
CharField toyota = Make(name='Toyota', factory_address='Somewhere in Japan...',
DateField email='toyota@toyota.com')
DateTimeField toyota.save()
DecimalField
EmailField camry = Car(name='Camry', active=True, price=29999.99, make=toyota)
FileField
camry.save()
FloatField
ImageField
IntegerField m
#Example Queries . co
TextField
d
nte
i
pr
Car.objects.get(pk=1) #get car with primary key = 1
Make.objects.all()
ty
#Relationship Fields Car.objects.filter(name='Camry') ret
Make.objects.all()[5:10] #offset 5, limit 10
://p
ForeignKey
Car.objects.order_by('name')
ttp
ManyToManyField Make.objects.filter(name__startswith='Toy') h
Car.objects.filter(make__name='Toyota') #relationship query

You might also like