🎸 Django Beginner

What are Django model field types?

Answer

Django provides many model field types mapping to database column types: String fields: CharField(max_length=200) — VARCHAR; TextField() — TEXT (unlimited); EmailField() — CharField with email validator; URLField(); SlugField() — URL-friendly slug; UUIDField(default=uuid.uuid4). Numeric fields: IntegerField(); BigIntegerField(); PositiveIntegerField(); FloatField(); DecimalField(max_digits=10, decimal_places=2) — for money; SmallIntegerField(). Boolean: BooleanField(default=False); NullBooleanField() (deprecated — use BooleanField(null=True)). Date/Time: DateField(auto_now_add=True) — set on creation; DateTimeField(auto_now=True) — update on every save; TimeField(); DurationField(). File: FileField(upload_to="uploads/"); ImageField(upload_to="images/") — requires Pillow. Choice fields: CharField(max_length=10, choices=[("draft","Draft"),("published","Published")]). Relationship fields: ForeignKey(User, on_delete=models.CASCADE, related_name="articles"); ManyToManyField(Tag, blank=True); OneToOneField(User, on_delete=models.CASCADE). JSON: JSONField(default=dict) — native JSON storage (PostgreSQL, MySQL 8+, SQLite 3.9+). Generic relations: GenericForeignKey — polymorphic FK. Common options: null=True (database NULL), blank=True (form validation), default=value, unique=True, db_index=True, verbose_name="Human Name".