user8744140
user8744140

Reputation:

executing django model query?

In django model query, i want to know the sequential execution of it. Consider a query Blog.objects.get(name='palm'). In this where the Blog is defined, is that same as class blog in models.py? What is objects i can't find anything related to this in source files of django. If Blog is a class, then what is the type of objects?

I want a development side concept. Can any one please explain how django makes these possible?

Upvotes: 0

Views: 84

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476659

Every non-abstract Django model class has an attribute objects attached to it (unless you of course explicitly remove it).

object is a Manager. It is an object that has a lot of methods to construct queries that are then send to the database to fetch/store data.

So you first access the objects manager of the Blog class, next you call .get(name='palm') on it. This means that Django will translate this into a query. This depends on the database system you use. For instance if it is MySQL it will look like:

SELECT name, some, other columns
FROM app_blog
WHERE name = 'palm'

The database will respond with zero, one or more rows, and Django will, in case no or more than one row is found, raise a DoesNotExists or MultipleObjectsReturned error. Otherwise it will load the data into a Blog object (by deserializing the columns into Python objects).

Upvotes: 2

Related Questions