Show Menu
Cheatography

Django Models Cheat Sheet by

Django Models

Field Types

AutoField
BigInt­ege­rField
Binary­Field
Boolea­nField
CharField
max_length
CommaS­epa­rat­edI­nte­ger­Field
max_length
DateField
auto_now
 
auto_n­ow_add
DateTi­meField
(as above)
Decima­lField
max_digits
 
decima­l_p­laces
EmailField
max_length
FileField
upload_to
 
storage
FloatField
ImageField
upload_to
 
height­_field
 
width_­field
Intege­rField
IPAddr­ess­Field
Generi­cIP­Add­res­sField
protocol
 
unpack­_ipv4
NullBo­ole­anField
Positi­veI­nte­ger­Field
Positi­veS­mal­lIn­teg­erField
SlugField
max_length
SmallI­nte­ger­Field
TextField
TimeField
auto_now
 
auto_n­ow_add
URLField
max_length
ForeignKey
model
 
relate­d_name
 
to_field
 
db_con­straint (True)
 
on_delete
ManyTo­Man­yField
model
 
limit_­cho­ices_to
OneToO­neField
 

Field Options

null
blank
choices
db_column
db_index
db_tab­lespace
default
editable
error_­mes­sages
help_text
primar­y_key
unique
unique­_fo­r_[­dat­e|m­ont­h|year]
verbos­e_name
validators

Field Lookups

__exact
__iexact
Case Insens­itive
__contains
__icon­tains
__in
Pass in a list
__gt, __gte, __lt, __lte
Comparison
__star­tswith, __ista­rts­with, __ends­with, __iend­swith
__range
(inclu­sive) =(X, Y)
__year, __month, __day, __hour, __minute, __second
__week_day
1=Sun, 7=Sat
__isnull
__regex

Related Manager Methods

add
x.ys_s­et.a­dd(y, ...)
create
x.ys_s­et.c­re­ate­(**­kwargs)
remove*
x.ys_s­et.r­em­ove(y1, y2, ...)
clear*
x.ys_s­et.c­lear()
 
*Only available when FK(nul­l=True)
 

QuerySet API

all
X.objects.
filter
filter­(**­kwargs)
exclude
exclud­e(**­kw­args)
annotate
annotate( models.Co­unt­('c­ost') )
order_by
[*fiel­ds_str]
reverse
distinct
values
*field­s_str
values­_list
*field­s_str
date(t­ime)?s
dates(­field, kind, order)
none
extra
extra(­sel­ect={})
get (returns 1 obj)
get( **kwargs )
create (returns 1 obj)
create( **kwargs )
get_or­_create (returns 1 obj)
get_or­_cr­eate( **kwargs )
count
latest, earliest, first, last
aggregate
aggregate( models.Co­unt­('e­ntry') )
delete

Meta Options

abstract
app_label
db_table
db_tab­lespace
get_la­test_by
managed
order_­wit­h_r­esp­ect_to
ordering
permis­sions
proxy
unique­_to­gether
index_­tog­ether
verbos­e_name
verbos­e_n­ame­_plural
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Python Cheat Sheet
          Intermediate Python Cheat Sheet