Field Types + Specific Options
<< boolean >>- BooleanField << True/False
- NullBooleanField << null/True/False
<< number >>
- IntegerField
- SmallIntegerField
- PositiveIntegerField
- PositiveSmallIntegerField
- BigIntegerField << 64-bit integer
- AutoField << auto-incremental IntegerField
- DecimalField
max_digits, decimal_places << 999.99 : max_digit=5, decimal_places=2
- FloatField
<< text >>
- CharField
max_length << length in characters
- TextField
- SlugField
- CommaSeparatedIntegerField
- URLField
verify_exists
- EmailField
max_length=75
- IPAddressField
- GenericIPAddressField
<< date/time >>
- DateField, DateTimeField, TimeField
auto_now=False << set current timestamp for every save
auto_now_add=False << set current timestamp when first create
<< file >>
- FileField << file upload field
upload_to << path appended to MEDIA_ROOT; can be callable object; can use strftime() formatting
max_length=100
- ImageField << subclass from FileField
height_field, width_field << auto-set
- FilePathField
<< relationship >>
- ForeignKey(one-sided model) << many-to-one relationship
limit_choices_to << effect Admin/ModelForm choices
e.g. limit_choices_to = {’pub_date__lte’: datetime.now}
related_name << backward relationship (1 --> many)
to_field
on_delete=models.CASCADE << emulate ON DELETE CASCADE behavior
e.g. user = models.ForeignKey(on_delete=models.SET_NULL) << when delete parent, set reference to NULL
CASCADE = also delete child when parent is deleted
PROTECT = cannot delete parent if child exists
SET_NULL
SET_DEFAULT = set to default value if parent deleted
SET()
DO_NOTHING
- ManyToManyField(other model) << many-to-many relationship
related_name << same as ForeignKey
limit_choices_to << same as ForeignKey
symmetrical << for self-reference
through << intermidiary model (for additional data)
db_table << table to store many-to-many relationship
- OneToOneField << one-to-one relationship
parent_link=False << True=link back to parent class (in subclass model)
(General) Field Options
- null=False << allow null in database- blank=False << for validation
- default << default value
- choices << value stored + value displayed
YEAR_IN_SCHOOL_CHOICES = (
(’FR’, ’Freshman’),
(’SO’, ’Sophomore’),
(’JR’, ’Junior’),
(’SR’, ’Senior’),
(’GR’, ’Graduate’),
)
MEDIA_CHOICES = (
(’Audio’, (
(’vinyl’, ’Vinyl’),
(’cd’, ’CD’),
)),
(’Video’, (
(’vhs’, ’VHS Tape’),
(’dvd’, ’DVD’),
)),
(’unknown’, ’Unknown’),
)
- db_column << column name in database (override auto-generated column name)
- db_index << True = create index for this field
- editable=True << False = not displayed for edit in form
- unique << True = unique value in field
unique_for_date, unique_for_month, unique_for_year
- primary_key << True = primary key for model (override auto-generated PK field)
- help_text << display with form widget
- verbose_name << human-readable name
Meta Options
- verbose_name, verbose_name_pluralhuman-readable name for model
- unique_together
combinated unique fields, e.g. unique_together = (("driver", "restaurant"),)
- abstract
True=model for abstract class
- db_table
database table used for model
- get_latest_by
DateField/DateTimeField used to identify latest data (use in manager's latest() method)
- ordering
default order when get, e.g. ordering = [’-order_date’] << order by order_date descending
- order_with_respect_to
True=children has order related to its parent (add additional column suffixed _order)
set at child model;
parent model can get ordered children using get_<RELATED>_order()
parent model can set children order using set_<RELATED>_order()
child model can use get_next_in_order(), get_previous_in_order()
- app_label
use when model exists outside standard models.py
ไม่มีความคิดเห็น:
แสดงความคิดเห็น