Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

Dynamic proxy models for Django

Project description

django_dynproxy
===============

Dynamic proxy models for Django

With django_dynproxy, you can dynamically modify a Django proxy model by :
* hiding one or many fields (from the concrete parent model)
* making mandatory one or many fields (who are not mandatory in the concrete parent model)

This can be useful when you want to use inheritance in your code, but not in your database

WARNING : THIS IS AN ALPHA RELEASE. NOT RECOMMENDED FOR PRODUCTION USE

Requirements :
--------------

django>=1.4.3

Installation :
--------------

pip install django_dynproxy

Usage :
---------

1) Create a concrete model

2) Create a Django proxy model deriving from the concrete model

3) Define a metaclass using dynproxy_metaclass_factory and specify :
- which base fields you want to exclude from your proxy model
- which non-mandatory base fields you want to be mandatory your proxy model

4) Add the __metaclass__ to your proxy model class


Sample :
---------

from dynproxy.utils import dynproxy_metaclass_factory

TRAVELER_TYPE_PILOT = '1'
TRAVELER_TYPE_PASSENGER = '2'

TRAVELER_TYPES = (
(TRAVELER_TYPE_PILOT,_(u"Pilot")),
(TRAVELER_TYPE_PASSENGER,_(u"Passenger"))
)

class Traveler(models.Model):
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
type = models.CharField(max_length=200,choices=TRAVELER_TYPES)
passport_number = models.CharField(max_length=200,blank=True)
trigram = models.CharField(max_length=200,blank=True)
pilot_category = models.ForeignKey(PilotCategory,null=True,blank=True)


def __unicode__(self):
return "%s %s" % (self.first_name,self.last_name)

def traveler_manager_factory(traveler_type):
class _TravelerManager(Manager):
def get_query_set(self):
return super(_TravelerManager,self).get_query_set().filter(type=traveler_type)
return _TravelerManager

PilotManager = traveler_manager_factory(TRAVELER_TYPE_PILOT)
PassengerManager = traveler_manager_factory(TRAVELER_TYPE_PASSENGER)


PilotMetaclass = dynproxy_metaclass_factory(
mandatory_fields=['trigram','pilot_category'],
fields_to_exclude=['passport_number']
)

PassengerMetaclass = dynproxy_metaclass_factory(
mandatory_fields = ['passport_number'],
fields_to_exclude =['trigram','pilot_category']
)


class Pilot(Traveler):
__metaclass__ = PilotMetaclass
objects = PilotManager()
class Meta:
proxy = True

class Passenger(Traveler):
__metaclass__ = PassengerMetaclass
objects = PassengerManager()
class Meta:
proxy = True

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django_dynproxy, version 0.2.0
Filename, size File type Python version Upload date Hashes
Filename, size django_dynproxy-0.2.0.zip (3.3 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page