Skip to content

net-prophet/django-easy-scoping

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Django Easy Scoping allows you to register scopes and aggregate functions on your Django models.

Installation

To get the ScopingMixin.py file you can download it or use pip.

Downloading File

Download the ScopingMixin file from github. ScopingMixin.py

Import ScopingMixin and ScopingQuerySet from ScopingMixin.py.

from <path to file>.ScopingMixin import ScopingMixin, ScopingQuerySet

Using pip

$ pip install django-easy-scoping

Import ScopingMixin and ScopingQuerySet from ScopingMixin.py.

from DjangoEasyScoping.ScopingMixin import ScopingMixin, ScopingQuerySet

Implementing

Mix ScopingMixin in with the Django model(s) you'd like to create scopes for.

For example, in the purchases model:

class Purchase(ScopingMixin, models.Model):

Override the Queryset for that model using ScopingQuerySet. By default, the override's name must be objects (see below for changing the default).

    objects = ScopingQuerySet.as_manager()

Done!

Implementing with existing Managers/Querysets

If you'd like to continue using your own custom manager/queryset then you can! You only need to take action if you'd like to replace the default name (objects) of the ScopingQuerySet override, for instance:

other_name = ScopingQuerySet.as_manager()

Then, simply open ScopingMixin.py in your sites-packages and edit the following methods on lines 6 and 11.

1  class ScopingMixin(object):
2
3      @classmethod
4      def get_scope(cls, name)
5          if hasattr(cls, '__scopes__') and name in cls.scopes():
6              return getattr(cls.objects.all(), name)
7
8      @classmethod
9      def get_aggregate(cls, name)
10         if hasattr(cls, '__aggregate__') and name in cls.aggregates():
11             return getattr(cls.objects.all(), name)

becomes

1  class ScopingMixin(object):
2
3      @classmethod
4      def get_scope(cls, name)
5          if hasattr(cls, '__scopes__') and name in cls.scopes():
6              return getattr(cls.other_name.all(), name)
7
8      @classmethod
9      def get_aggregate(cls, name)
10         if hasattr(cls, '__aggregate__') and name in cls.aggregates():
11             return getattr(cls.other_name.all(), name)

About

Allows you to register scopes and custom aggregate functions on your Django models.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published