0% found this document useful (0 votes)
824 views1 page

Django Cheatsheet PDF

This document provides a cheat sheet and quick reference for Django, including examples of database engines, model fields, relationships between models, and example queries. It shows how to define models with fields like CharField, ForeignKey, create model objects, and perform queries to filter, order, and retrieve objects from the database. Useful links to the Django website and tutorials are also included.

Uploaded by

Tikam Suvasiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
824 views1 page

Django Cheatsheet PDF

This document provides a cheat sheet and quick reference for Django, including examples of database engines, model fields, relationships between models, and example queries. It shows how to define models with fields like CharField, ForeignKey, create model objects, and perform queries to filter, order, and retrieve objects from the database. Useful links to the Django website and tutorials are also included.

Uploaded by

Tikam Suvasiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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='[email protected]')
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