Django Model managers and properties

from django.db import modelsclass User(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField()
def __str__(self):
from app1.models import Useruser = User(name="xxx", email="")
from app1.models import Useruser = User.objects.create(name="xxx", email="")
from django.db.models import Model, Managerclass User(Model):
objects = Manager() # The default Manager.
from django.db import modelsclass BookManager(models.Manager):
def title_count(self, keyword):
return self.filter(title__icontains=keyword).count()
class Book(models.Model):
title = models.CharField(max_length=100)
publication_date = models.DateField()
num_pages = models.IntegerField(blank=True, null=True)
objects = BookManager() # The Custom Manager. def __unicode__(self):
return self.title
from django.db import modelsclass Person(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
def _get_full_name(self):
return '%s %s' % (self.first_name, self.last_name) # Returns the person's full name.
full_name = property(_get_full_name)

def __unicode__(self):
return self.full_name
person = Person.objects.get(first_name='xyz', last_name='abc')person.full_name  # Note this isn't a method -- it's treated as an attribute, It returns 'xyz abc'



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store