django-qmethod

Software Screenshot:
django-qmethod
Software Details:
Version: 0.0.3
Upload Date: 14 Apr 15
Developer: Zachary Voase
Distribution Type: Freeware
Downloads: 1

Rating: nan/5 (Total Votes: 0)

django-qmethod is a Django app for easily defining operations on collections of Django models (that is, QuerySets and Managers).

One day, I hope something like this is included in Django core.

Usage

Basic usage is as follows:

import cPickle as pickle
from django.db import models
from djqmethod import Manager, querymethod

class Group(models.Model):
 pass

class Person(models.Model):
 GENDERS = dict(m='Male', f='Female', u='Unspecified').items()

 group = models.ForeignKey(Group, related_name='people')
 gender = models.CharField(max_length=1, choices=GENDERS)
 age = models.PositiveIntegerField()

 # Note: you need to create an explicit manager here.
 objects = Manager()

 @querymethod
 def minors(query):
 return query.filter(age__lt=18)

 @querymethod
 def adults(query):
 return query.filter(age__gte=18)

# The `minors()` and `adults()` methods will be available on the manager:
assert isinstance(Person.objects.minors(), models.query.QuerySet)

# They'll be available on subsequent querysets:
assert isinstance(Person.objects.filter(gender='m').minors(),
 models.query.QuerySet)

# They'll also be available on relations, if they were mixed in to the
# default manager for that model:
group = Group.objects.all()[0]
assert isinstance(group.people.minors(), models.query.QuerySet)

# The QuerySets produced are totally pickle-safe:
assert isinstance(pickle.loads(pickle.dumps(Person.objects.minors())),
 models.query.QuerySet)


A test project is located in test/example/; consult this for a more comprehensive example.

Installation

pip install django-qmethod

Requirements:

  • Python
  • Django

Other Software of Developer Zachary Voase

Markdoc
Markdoc

11 May 15

django-boss
django-boss

12 May 15

Markdoc
Markdoc

21 Jul 15

Dagny
Dagny

20 Feb 15

Comments to django-qmethod

Comments not found
Add Comment
Turn on images!