Wednesday, 14 July 2021

*Episode 8* DJANGO (Models of Django)


 Models


    Models are a class that is used for representation of a collection of a table in a DB. In this case, each of the attribute of a class is a field in the collection or table. In Django, models are defined in the file “app/models.py”.


  •  How to create a Model :-

    Consider the example given below:


from django.db import models

class Dreamreal (models.Model):

web = models.CharField(max_length = 40)

mail = models.CharField(max_length = 40)

name = models.CharField(max_length = 40)

phnumber = models.IntegerField()

class Meta:

db_table = “dreamreal”


     Each model has to inherit from the class “django.db.models.Model”. Note that in the above class, we have defined 4 attributes that will form the fields for our table.


     Once the model has been created, the Django should then generate our actual database. The following code can be used:


$python manage.py syncdb


  •  Manipulating Data (CRUD) :-

     It is possible for us to perform CRUD operations on our models. Consider the code given below for “myapplication/views.py”:


from myapplication.models import Dreamreal

from django.http import HttpResponse

def crudops(request):

#Creating the entry

dreal = Dreamreal(

web = “www.mywebsite.com”, mail = “john@mywebsite.com”,

name = “john”, phnumber = “0725626821”

)

dreal.save()

#Reading ALL the entries

objects = Dreamreal.objects.all()

res =‘Printing all the Dreamreal entries in our DB : <br>’

for e in objects:

res += e.name+”<br>”

#Reading a specific entry:

john = Dreamreal.objects.get(name = “john”)

res += ‘Printing a single entry <br>’

res += john.name

#Delete the entry

res += ‘<br> Deleting the entry <br>’

john.delete()

#Update

dreal = Dreamreal(

web = “www.mywebsite.com”, mail = “john@mywebsite.com”,

name = “john”, phnumber = “0725626821”

)

dreal.save()

res += ‘Updating the entry<br>’

dreal = Dreamreal.objects.get(name = ‘john’)

dreal.name = ‘joel’

dreal.save()

return HttpResponse(res)


  •  Other Data Manipulations :-

     There are also some other types of manipulations that can be done on models. The previous CRUD operations were performed on the instances of our model. However, we do not need to perform our operations directly on the class that represents our model. We want to create a view “datamanip” in the file “myapplication/views.py”. Here is the code for doing this:


from myapplication.models import Dreamreal

from django.http import HttpResponse

def datamanip (request):

res = ”

#Filtering the data:

qs = Dreamreal.objects.filter(name = “john”)

res += “Found : %s results<br>”%len(qs)

#Ordering the results

qs = Dreamreal.objects.order_by(“name”)

for e in qs:

res += e.name + ‘<br>’

return HttpResponse(res)


  •  Linking Models :-

     There are 3 ways in Django by which models can be linked together. The first method involves the use of the “one-to-many” relationship. For us to define the relationship, we have to use the “django.db.models.ForeignKey”. This is shown in the code given below:


from django.db import models

class Dreamreal(models.Model):

web = models.CharField(max_length = 40)

mail = models.CharField(max_length = 40)

name = models.CharField(max_length = 40)

phnumber = models.IntegerField()

online = models.ForeignKey(‘Online’, default = 1)

class Meta:

db_table = “dreal”

class Online(models.Model):

domain = models.CharField(max_length = 20)

class Meta:

db_table = “onlineTable”


👈Episode 7(D).                                         Episode 9(D)👉

Share This Post

PRINT THIS POST

No comments:

Post a Comment

If you have any doubts. Please let me know.

Featured post

*Episode 1* MCQ for Govt. Job/ Private Job/ MNCs

  Topic:- One Word Substitution 1) Especially skilled in storytelling  Answer:- Raconteur 2) Fear of loneliness Answer:- Eremophobia  3) Usa...