lucasrf27
lucasrf27

Reputation: 360

Querysets on views or on Model Managers DJANGO

Some days ago a guy explained me that on ruby on rails the queries are done on models. Because it gets already saved at your data before be requested on views and the query.

By the way I've learned and had been working until now, I'm setting the query on views.py and passing, through a context variable. So I started to read about Model.Manager and still didn't find a answer to which way is better:

  1. queries made on views
  2. queries made by simple functions on models
  3. queries made on models.Manager class for each model

Upvotes: 0

Views: 2152

Answers (4)

Andrii Matiiash
Andrii Matiiash

Reputation: 568

According to the Django idioms want to add some advice that can be useful:

  • model managers it's a place for most common queries, not for all queries. Managers describe basic methods for working with models. If you have a widely-used logic for your model with queryset - put it to the manager's method. For example, my users split by domains, so I want to get the user by name and domain, so add the get_by_name_and_domain method to the user model manager. Also, you can access your model by the model attribute in the manager.
  • models are the part of MTV (Model-View-Template) model on Django and the main purpose of the 'model' itself is to describe db-object but not related business logic. So put your custom queries to views and remember about DRY principle.

Upvotes: 1

bruno desthuilliers
bruno desthuilliers

Reputation: 77892

  1. Use custom QuerySets and ModelManagers in your models
  2. call your model's ModelManager custom methods from within your views
  3. pass the returned values (querysets, instances or whatever) to the templates (or JSON serializer etc)

It's a matter of separation of concerns:

  1. the template (or json serializer or whatever) doesn't have to know where data come from nor they were obtained - only what the data structure is
  2. views don't have to know about how the query is implemented, only on how to get relevant data
  3. only the model layer should know about it's implementation (encapsulation 101)

Upvotes: 1

Pramod Naik
Pramod Naik

Reputation: 11

Click here for Documentation

Ref.

By default, Django adds a Manager with the name objects to every Django model class. If u have specific business logic, you can make use of managers to override built-in model methods like save() and delete() to add business logic to default database behaviour or you can specifically design some query logic.

file name ---- > models.py

from .managers import ModelNameManager

class ModelName(Base):
      title = models.CharField(max_length=255, blank=True, null=True)
      headline = models.CharField(max_length=255, blank=True, null=True)


      objects = ModelNameManager()

create a file managers.py file in your application file name ---- > managers.py

class ModelNameQuerySet(models.QuerySet):
    def by_name(self, id):
        return self.filter(id=id)

class ModelNameManager(models.Manager):
    def get_queryset(self):
        return ModelNameQuerySet(self.model, using=self._db)

    def by_name(self, ad):
        return self.get_queryset().by_name(id)

in Views.py or any services file file your query will be

     import ModelName
     obj = ModelName.get_queryset(id)
     obj.title

this will return the object based on the query written in managers.

I hope this is helpful.

Upvotes: 1

upendra243
upendra243

Reputation: 101

Every model is associated with a Manager (default one is objects)

>>> from django.contrib.auth.models import User
>>> user = User.objects.all()
>>> type(user)
<class 'django.db.models.query.QuerySet'>

When you need to make any queries to the model you need a manager for this. In the above example user doing a query on objects manger of User Model.

3-queries made on models.Manager class for each model - correct interpretation

Upvotes: 1

Related Questions