
A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table.
The basics:
Each model is a Python class that subclasses django.db.models.Model.
Each attribute of the model represents a database field.
With all of this, Django gives you an automatically-generated database-access API
from django.db import models
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
first_name and last_name are fields of the model. Each field is specified as a class attribute, and each attribute maps to a database column.
The above Person model would create a database table like this:
CREATE TABLE myapp_person (
"id" serial NOT NULL PRIMARY KEY,
"first_name" varchar(30) NOT NULL,
"last_name" varchar(30) NOT NULL
);
Once you have defined your models, you need to tell Django you’re going to use those models. Do this by editing your settings file and changing the INSTALLED_APPS setting to add the name of the module that contains your models.py.
For example, if the models for your application live in the module myapp.models, INSTALLED_APPS should read, in part:
INSTALLED_APPS = [
#...
'myapp',
#...
]
When you add new apps to INSTALLED_APPS, be sure to run manage.py migrate, optionally making migrations for them first with manage.py makemigrations.
first_name = models.CharField(max_length=30, primary_key=True) id = models.BigAutoField(primary_key=True) phone =models.CharField(max_length=30, unique=True) address =models.CharField(max_length=30, NULL=True) default is false first_name = models.CharField(blank=True, max_length=30)
Step 1: create app - python manage.py startapp mymodel
Step 2: register your app in installed_apps
Step 3: python manage.py makemigrations
Step 4: python manage.py migrate
models.py
from django.db import models
class MyModel(models.Model):
title=models.CharField(max_length=30)
description=models.TextField()
To register in admin:
admin.py
from django.contrib import admin
from mymodelapp.models import MyModel
admin.site.register(MyModel)
or
class MyModelAdmin(admin.ModelAdmin):
fields = ['title', 'description']
admin.site.register(MyModel,MyModelAdmin)
views.py
from mymodelapp.models import MyModel
def home(request):
modelData=MyModel.objects.all()
data={
'modelData':modelData
}
return render(request,'home.html',data)
home.html
{% for n in modelData%}
<div>
{{n.title}}
{{n.description}}
</div>
{% endfor %}
Arrange data in ascending or descending order
views.py
modelData=MyModel.objects.all().order_by('title')
modelData=MyModel.objects.all().order_by('-title')
Limit query result
views.py
modelData=MyModel.objects.all()[:3]
modelData=MyModel.objects.all().order_by('title')[:3]
modelData=MyModel.objects.all().order_by('title')[2:5]
Django Template filter: safe - Marks a string as not requiring further HTML escaping prior to output.
home.html
{{n.description | safe}}
Django Template filter: lower - Converts a string into all lowercase.
home.html
{{n.description | lower}}
Django Template filter: upper - Converts a string into all uppercase.
home.html
{{n.description | upper}}
{{n.description | upper | safe}}
Django Template filter: capfirst - Capitalizes the first character of the value. If the first character is not a letter, this filter has no effect.
home.html
{{n.description | capfirst}}
Django Template filter: first - Returns the first item in a list.
home.html
{{n.description | first}}
Django Template filter: length - Returns the length of the value. This works for both strings and lists.
home.html
{{n.description | length}}
Django Template filter: wordcount - Returns the number of words.
home.html
{{n.description | wordcount}}
Django Template filter: slice - Returns a slice of the list.
home.html
{{n.description | slice:"0:3"}}
views.py
def home(request):
modelData=MyModel.objects.all()
if request.method=="GET":
filterData = request.GET.get('inputboxname');
if filterData!=None:
modelData=MyModel.objects.filter(title=filterData)
data={
'modelData':modelData
}
return render(request,'home.html',modelData)
modelData=MyModel.objects.filter(title__icontains=filterData)
{% empty %}
No Data Found
Ad: