Django
Model Definition
Define a Django model with field types and meta options.
By EZ4Code Team
modelormfields
Code
from django.db import models
class Article(models.Model):
title = models.CharField(max_length=200)
body = models.TextField()
published_at = models.DateTimeField(auto_now_add=True)
is_published = models.BooleanField(default=False)
views = models.PositiveIntegerField(default=0)
class Meta:
ordering = ["-published_at"]
verbose_name_plural = "Articles"
def __str__(self):
return self.titleExplanation
Django models subclass models.Model and declare typed fields that map to database columns. CharField requires max_length, TextField is for long text, and DateTimeField with auto_now_add sets the timestamp on creation. The inner Meta class configures default ordering and admin display options.
More Django Snippets
ORM QuerySet
Filter, exclude, annotate and chain QuerySets.
Class-Based Views
Use generic class-based views for common CRUD flows.
URL Routing
Wire URLs to views with path converters and includes.
ModelForms
Build forms from models with validation and widgets.
Template Tags
Use built-in tags and filters in Django templates.
Admin Customization
Customize the Django admin with list_display and actions.