The goal of this in-progress repository will be to concisely capture / document common, core Django tools. Where appropriate, code samples will be added to highlight common use cases. Links to popular Stack Overflow answers will also be added to further explain topics.
- Tags
- Filters
- Date formats
- Field options (kwargs)
- Fields
Meta
attributes- abstract
- app_label
- db_table
- get_latest_by
- managed
- ordering
- permissions
- proxy
- unique_together
- index_together
- verbose_name
- verbose_name_plural
- Validators
- django.db.models.signals
- django.core.signals
- etc.
- Methods
- all()
- none()
filter(query)
exclude(query)
order_by(field...)
- etc.
- Queries (lookups)
- exact
- iexact
- contains
- gt
- etc.
- Aggregation functions
- Common kwargs
- required
- label
- help_text
- initial
- validators
- widget
- error_messages
- localize
- Fields
- Boolean
- Strings
- Choices
- Dates & Times
- Numbers
- Files
- Internet
- Misc
- Validators
- Views
- Shortcuts
- Decorators
- GCBV
- Formsets
- Widgets
- ModelForm & ModelFormSet
- ModelAdmin
- Options and callbacks
- InlineModelAdmin
- AdminSite
- Migrations
- Shell
- Server
- etc.
- The
request
object - Custom user models
- File upload example
null
vs.blank
β Differences, and when to use them
Option | Type | Default |
---|---|---|
null |
boolean | False |
blank |
boolean | False |
choices |
iterable (list or tuple) | None |
db_column |
string | None |
db_index |
boolean | False |
db_tablespace |
string | None |
default |
value or callable | NOT_PROVIDED |
editable |
boolean | True |
error_messages |
dictionary | None |
help_text |
string | '' (empty string) |
primary_key |
boolean | False |
unique |
boolean | False |
unique_for_date |
string (DateField or DateTimeField name) |
None |
unique_for_month |
string (DateField or DateTimeField name) |
None |
unique_for_year |
string (DateField or DateTimeField name) |
None |
verbose_name |
string | None |
validators |
list | [] (empty list) |
| Field Name | Options | Notes |
|------------|------------------|----------|-|-|
| AutoField
| | You usually wonβt need to use this directly. |
| BigIntegerField
| | For integers between -9223372036854775808 and 9223372036854775807 |
| BinaryField
| | Reminder: don't store files in the db |
| BooleanField
| | A true / false field. Use NullBooleanField
to accept NULL
values |
| CharField
| max_length=None
| For small- to medium-sized strings |
| CommaSeparatedIntegerField
| max_length=None
| |
| DateField
| [auto_now=False
]
[auto_now_add=False
] | |
| DateTimeField
| [auto_now=False
]
[auto_now_add=False
] | |
| DecimalField
| max_digits=None
decimal_places=None
| Uses Python's Decimal
type |
| DurationField
| | Modeled in Python by timedelta
|
| EmailField
| [max_length=254
] | Uses EmailValidator
to validate input |
| FileField
| [upload_to=None
]
[max_length=100
] | |
| FilePathField
| path=None
[match=None
]
[recursive=False
]
[max_length=100
] | |
| FloatField
| | Uses Python's float
type |
| ForeignKey
| othermodel
[ondelete
]
[related_name
] | A many-to-one relationship. See additional arguments in the docs. |
| GenericIPAddressField
| [protocol='both'
]
[unpack_ipv4=False
] | If blank=True
, null
must be True
|
| ImageField
| [upload_to=None
]
[height_field=None
]
[width_field=None
]
[max_length=100
] | Requires the Pillow library |
| IntegerField
| | For integers between -2147483648 and 2147483647 |
| ManyToManyField
| othermodel
[related_name
] | See additional arguments in the docs. |
| NullBooleanField
| | Like BooleanField
, but allows NULL
values by default |
| OneToOneField
| othermodel
[ondelete
]
[parent_link=False
]
[related_name
] | |
| PositiveIntegerField
| | Like an IntegerField
, but must be either positive or zero (0). |
| PositiveSmallIntegerField
| | A PositiveIntegerField
for small values. Values from 0 to 32767 are safe in all databases. |
| SlugField
| [max_length=50
]
[allow_unicode=False
] | |
| SmallIntegerField
| | Integers between -32768 and 32767 are safe in all databases. |
| TextField
| | A large text field. max_length
is not enforced at the db level. |
| TimeField
| [auto_now=False
]
[auto_now_add=False
] | Represented in Python by a datetime.time
instance. |
| URLField
| [max_length=200
] | A CharField
for URLs |
| UUIDField
| | | |