DEV Community

loading...
Cover image for 👨‍💻 Basic operations on Models In Django ( Part 6 ) - Django For Beginners

👨‍💻 Basic operations on Models In Django ( Part 6 ) - Django For Beginners

ketanip profile image KetanIP Updated on ・3 min read

This is part of the series Django For Beginners we are going to see about apps in app and we will see about models in django in this part.

In this post we are going to learn about queries in django. When we use django we do not need to write SQL Queries it is done by django itself, we write super user friendly syntax for the queries ,

Let's see we want to get all the objects in a model ( note each model is a table, it may be more depending on the type of relation used while creating it ), let the schema of this model be simple it will be as follows,

from django.db import models

class Book(models.Model):
    title       = models.CharField(max_length=250)
    createdAt   = models.DateTimeField(default=timezone.now)
    description = models.TextField()
    orders      = models.PositiveItegerField(default=0)

Enter fullscreen mode Exit fullscreen mode

Now to get it all we use the following syntax,

allBooks = Book.objects.all()
Enter fullscreen mode Exit fullscreen mode

and that's it will return all the books of QueryList form.

How to run Queries in Django ?

I should have told it first I know but I told it now because you should understand how easy it is to write queries in django. Now let's see how to run them,

There are two ways,

  1. Interactive Way ( in command line ).
  2. In any python file.

Interactive Way

To use it interactively just go to your root directory of your django project and type the following command to start the interactive console,

python manage.py shell
Enter fullscreen mode Exit fullscreen mode

It will start the interactive console from which you can import any model in the project, you can access all of the project files. Also you can also import normal python libraries.

Regular Way

You can import models to any file in the project and can use it as per your will and in later part of this series we are going to use in this way in views.py to write views for our website.


Now let's see the basic operations on models,,

To Create New Model Object

To create new model object we first import it and then we use create method which is build in model on it and create it. Let me show a example to you,

from myapp.models import Book

newBook = Book.objects.create(title="Harry Potter - Some Part", description="Don't you know it ?")
newBook.save()
Enter fullscreen mode Exit fullscreen mode

As you may have seen that creating models is simple damn simple. We just need to pass values in objects.create method of that model and store it in variable and than use save method on it. It is as simple as it could get.

You may have seen that I have not passed certain parameters, such as createdAt and orders, because we have provided default values to it. But we can also pass it. Let me show you how,

from myapp.models import Book
from datetime import datetime
from random import randint

currentDateTime = datetime.now()
orders = randint(0,10)

newBook = Book.objects.create(title="Harry Potter - Some Part", description="Don't you know it ?", createdOn=currentDateTime, orders=orders)
newBook.save()
Enter fullscreen mode Exit fullscreen mode

This is how it is done. You may have seen that I have passed createdOn in third place but while creating model it was in 2^nd^ place I did it to show that order in which you pass these keyword arguments doesn't matter.


Query The Object

Now comes the part of querying the model, here I will cover basics as querying is a very big topic, with the part I cover here would e more than enough for many project , at least in the beginning. Now let's start,

Sorry to interrupt you but I want to tell you one thing that I have started a small blog you may continue reading it here it will help me a lot.

Discussion (2)

pic
Editor guide
Collapse
sm0ke profile image
Sm0ke

Nice. Thank you!

Collapse
ketanip profile image
KetanIP Author

Welcome Sir.