Ch-11 Getting Started With DJango
Ch-11 Getting Started With DJango
1. models.py:
● A model is a class that represents a table or collection in our DB, and
where every attribute of the class is a field of the table or collection.
● Models are defined in the app/models.py
● This file can contain multiple models
● Every model inherits from django.db.models.Model.
● In short, Django Models is the SQL of Database one uses with Django.
● SQL (Structured Query Language) is complex and involves a lot of
different queries for creating, deleting, updating or any other stuff
related to databases.
● Django models simplify the tasks and organize tables into models.
Generally, each model maps to a single database table.
Basics of a model include –
Syntax
class ModelName(models.Model):
field_name = models.Field(**options)
Open the models.py file. It is almost empty by default, with only an import
statement and a comment:
models.py:
from django.db import models
To add a Members table in our database, start by creating a Members class, and
describe the table fields in it:
class Members(models.Model):
username = models.CharField(max_length=255)
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
email = models.CharField(max_length=255)
Whenever we are creating, deleting or updating a model, 2 commands should
be executed:
Makemigrations: generates SQL command for already installed apps and
nelly created apps
Python manage.py makemigrations
Django lets us interact with its database models, i.e. add, delete, modify and
query objects, using a database-abstraction API called ORM(Object Relational
Mapper). We can access the Django ORM by running the following command
inside our project directory.
Step 1. python manage.py shell
Now the Python interpreter (Python shell) is running.
(InteractiveConsole)
>>>
Step 2. At the bottom, after the three >>> write the following
>>> from Members.models import Members
Step 5: Execute this command to see if the Members table got a member:
>>> Members.objects.all().values()
Result will be:
<QuerySet [{'id': 1, ‘username’: ‘raj22’, 'firstname': 'Raj',
'lastname': 'Patil', ‘email’: ‘[email protected]’}]>
View in Browser
To see the result in a web page, we can create a view.open the views.py file
from django.http import HttpResponse
from django.template import loader
from .models import Members
def index(request):
mymembers = Members.objects.all().values()
output = ""
for x in mymembers:
output += x["firstname"]
return HttpResponse(output)
Navigate to the project folder and type this to start the server:
py manage.py runserver
● One of the most powerful parts of Django is the automatic admin interface.
● It reads metadata from your models to provide a quick, model-centric
interface where trusted users can manage content on your site.
● Django comes with a built-in authentication system.
● The authentication system includes users, groups, and permissions.
● When a model is created, Django will automatically create four default
permissions for the following actions:
1. add: Users with this permission can add an instance of the model.
2. delete: Users with this permission can delete an instance of the
model.
3. change: Users with this permission can update an instance of the
model.
4. view: Users with this permission can view instances of this model.
● To create an admin user type below command:
○ python manage.py createsuperuser
○ Username (leave blank to use 'admin'): reshma
○ Email address: [email protected]
○ Password:
○ Password (again):
○ Superuser created successfully.
● Run your server in the background in bash by command python manage.py
runserver. Head over to the browser and type the following in the URL.
● http://127.0.0.1:8000/admin
● Fill out your details afterward, i.e., the username and password that you've
created earlier:
View your admin panel afterward with our newly created models ''Students”.
Change the content of the 'Students’' by clicking the 'Add' button. Fill out the
information and 'Save' the details.
Edit or Delete the Student by clicking on a particular student object.