more on manager methods

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
from django.db import models
from django.contrib.auth.models import User

################ models ####################

class AccountManager(models.Manager):
    def belongs_to_user(self, user=None):
        qs = super(type(self), self).get_query_set()
        if user:
            return qs.filter(users__username=user)
        else:
            return qs

class Account(models.Model):
    title = models.CharField(maxlength=30, blank=False)
    users = models.ManyToManyField(User, blank=True, null=True)
    objects = AccountManager()

################ views ####################

def user_accounts(request):
    user_acct_qs = Account.objects.belongs_to_user(request.user.username)

More like this

  1. Using manager methods by ubernostrum 6 years, 2 months ago
  2. OwnerField by dune2 5 years, 5 months ago
  3. Tatsypie: additional list endpoints for custom Model's manager methods by migajek 6 months, 1 week ago
  4. Edit users on Group admin by cedriccollins 1 year, 11 months ago
  5. Custom Model Manager Chaining by hunterford 2 years, 10 months ago

Comments

adurdin (on March 1, 2007):

The first line of belongs_to_user() would be better as:

qs = self.get_query_set()

#

(Forgotten your password?)