So I am interested in using the example code below in a Django project, but am puzzled when trying to understand how I would call one model manager explicitly.
class AuthorManager(models.Manager):
def get_queryset(self):
return super(AuthorManager, self).get_queryset().filter(role='A')
class EditorManager(models.Manager):
def get_queryset(self):
return super(EditorManager, self).get_queryset().filter(role='E')
class Person(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
role = models.CharField(max_length=1, choices=(('A', _('Author')), ('E', _('Editor'))))
people = models.Manager()
authors = AuthorManager()
editors = EditorManager()
I know the view calls the models and the models calls the model manager, but its kind of confusing to me. Can I specify which view calls which manager or does the model take care of that implicitly through some other way?
As per the documentation, you can call either model manager from your views with Person.authors.get_queryset
or Person.editor.get_queryset
. Additionally, you can pass data back to either manager. For example:
In views:
data = {
'first_name' : request.POST['first_name'],
'last_name' :request.POST['last_name'],
'email' : request.POST['email'],
}
author = Person.authors.new_author(data)
editor = Person.editor.new_editor(data)
The "author"/"editor" variable above will become whatever your models passes back.
Obviously, you would want to define new methods in your managers. E.g.
In models:
class AuthorManager(models.Manager):
def new_author(self):
Person.authors.create(...)
class EditorManager(models.Manager):
def new_editor(self):
Person.editors.create(...)
You can choose to have your models pass back whatever user info you need to your views.