django-qmethod

Define methods on QuerySets without custom manager and QuerySet subclasses
Download

django-qmethod Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Public Domain
  • Price:
  • FREE
  • Publisher Name:
  • Zachary Voase
  • Publisher web site:
  • http://github.com/disturbyte/

django-qmethod Tags


django-qmethod Description

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.UsageBasic usage is as follows:import cPickle as picklefrom django.db import modelsfrom djqmethod import Manager, querymethodclass Group(models.Model): passclass 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()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.Installationpip install django-qmethodProduct's homepage


django-qmethod Related Software