top of page

How to create Django Model ? | Create Model and insert Object data using Django Shell

May 20, 2019
2 min read

What is model in django ?


A model is a class that represents 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 Create_Model_App/models.py


Dreamreal model created as an example:

model.py:

from django.db import models

class Dreamreal(models.Model):

website = models.CharField(max_length = 50)

mail = models.CharField(max_length = 50)

name = models.CharField(max_length = 50)

phonenumber = models.IntegerField()

class Meta:

db_table = "dreamreal"

And then after use command :

>python manage.py makemigrations

Output:


If you make any changes in module object then run this code in Django shell script:

>python manage.py migrate


Start shell in cmd:

>python manage.py shell

Import Module class as:

>form create_model_App.models import Dreamreal,Online


Now table is created and we enter and display data form this table:


Check table record by id. It means we can check what numbers of time we will added record


Another way to save record in a table: First define a model and entered record one by one in models or django tables.

> a = Dreamreal()

> a.website = "www.codersarts.com"

> a.mail = "codersarts@gmail.com"

> a.name = "jitendra"

> a.phonenumber = "9999999999"

> a.save()

If we want to display data with group means one object link with another objects then we add these line in our code:

Model.py

from django.db import models

class Dreamreal(models.Model): website = models.CharField(max_length = 50) mail = models.CharField(max_length = 50) name = models.CharField(max_length = 50) phonenumber = models.IntegerField() def __str__(self): return self.website + '----' + self.mail

After change code first we exit( by using( >exit )command) from shell then and back to database API by using these commands:


Now import show result again:


Filteration using filter to specify output:

Use this in python shell:

>Dreamreal.objects.filter(id=1)

To see structure of Dreamreal module add some line in admin.py.

Admin.py:

from django.contrib import admin

from .models import Dreamreal

# Register your models here. admin.site.register(Dreamreal)

output result:


Click on Dreamreals

output show all previous add data which added through view.py or shell

If you want to make any changes in data then click one of a the object. And make changes by this.

If you like Codersarts blog and looking for Assignment help,Project help, Programming tutors help and suggestion you can send mail at contact@codersarts.com

For more details or any project help contact coders arts experts or team by click here.

 
 
 

Recent Posts

See All
AI Employee Procurement and Purchase Request Agent

Make it easier for employees to request the goods and services they need while keeping purchasing within company policy. An AI Employee Procurement and Purchase Request Agent provides a conversational

 
 
 
AI Event Registration and Attendee Data Assistant

Event registration is one of the first interactions attendees have with an event organizer. Whether someone is registering for a conference, purchasing an exhibition pass, signing up for a corporate e

 
 
 

Comments


bottom of page