Coder Social home page Coder Social logo

charettes / django-delegate Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fish2000/django-delegate

0.0 3.0 0.0 127 KB

Automatic delegate methods for Django managers and querysets without runtime dispatch penalties.

Home Page: http://pypi.python.org/pypi/django-delegate

License: BSD 3-Clause "New" or "Revised" License

Python 100.00%

django-delegate's Introduction

image

django-delegate

With django-delegate, you get AUTOMATICALLY CHAINABLE MANAGER/QUERYSET DELEGATE METHODS.

Normally, by defining manager methods, Django lets you do this:

>>> SomeModel.objects.custom_query()

... but it WON'T let you do this:

>>> SomeModel.objects.custom_query().another_custom_query()

... unless you duplicate your methods and define a redundant queryset subclass... UNTIL NOW.

With DelegateManager and @delegate, you can write maintainable custom-query logic with free chaining. instead of defining manager methods, you define queryset methods, decorate those you'd like to delegate, and a two-line DelegateManager subclass specifying the queryset. ET VIOLA. Like so:

from delegate import DelegateManager, delegate

class CustomQuerySet(models.query.QuerySet):

    @delegate
    def qs_method(self, some_value):
        return self.filter(some_param__icontains=some_value)

    def dont_delegate_me(self):
        return self.filter(some_other_param="something else")

class CustomManager(DelegateManager):
    __queryset__ = CustomQuerySet

class SomeModel(models.Model):
    objects = CustomManager()


# this will work:
SomeModel.objects.qs_method('yo dogg')
# this will also work:
SomeModel.objects.qs_method('yo dogg').qs_method('i heard you like queryset method delegation')

To delegate all of the methods in a QuerySet automatically, you can create a subclass of DelegateQuerySet. These two QuerySet subclasses work identically:

from delegate import DelegateQuerySet, delegate

class ManualDelegator(models.query.QuerySet):
    @delegate
    def qs_method(self):
        # ...

class AutomaticDelegator(DelegateQuerySet):
    def qs_method(self):
        # ...

You can also apply the @delegate decorator directly to a class -- this permits you to delegate all the methods in a class without disrupting its inheritance chain. This example works identically to the previous two:

from delegate import delegate

@delegate
class CustomQuerySet(models.query.QuerySet):

    def qs_method(self, some_value):
        return self.filter(some_param__icontains=some_value)

django-delegate's People

Contributors

fish2000 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.