Skip to main content

django-generic-models provides subclassed models with a clear API for fetching superclasses, subclasses and making comparison.

Project description


.. image::

django-generic-models provides subclassed models with a clearer API for fetching superclasses, subclasses and making comparison.


* Python 3.4, 3.5 and 3.6.
* Django 1.8, 1.9, 1.10, 1.11 and 2.0.



$ pip install django-generic-models

Basic usage

Declare the superclass as a subclass of ``TypedModel``::

from genericmodels.models import TypedModel

class Animal(TypedModel):
name = CharField(max_length=30)

def say_something(self):
raise NotImplementedError

Then, declare the subclasses as usual::

from django.db.models import CharField

class Cat(Animal):
color = CharField(max_length=30)

def say_something(self):
return 'meow'

You'll be able to get the specific instance of ``Animal`` or the generic instance of ``Cat`` very easily with::

cat = Cat()
animal = cat.generic
cat = animal.specific

Comparison also works::

>>> print(cat == animal)

Even though they are not the same object in memory, and one is a ``Cat`` instance and the other is an ``Animal`` instance, they are still the same object, semantically.

For an example of working code, check the ``test_app``.


Install for development and with dependencies::

$ pip install -e .[dev]

Asserting code quality


$ python quality

Running the tests

To run the tests, use::

$ python test

To run the tests with ``tox``::

$ tox

Packaging for PyPI distribution


$ python package

The build will happen if the repository is clean: no untracked files are found. If not, you can see what's wrong with ``git clean -dfx --dry-run`` and remove or track and commit those files.

Builds will be available at ``dist``, in wheel and source.


Pull requests are welcome.

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-generic-models, version 0.3
Filename, size & hash File type Python version Upload date
django_generic_models-0.3-py3-none-any.whl (5.9 kB) View hashes Wheel py3
django-generic-models-0.3.tar.gz (5.4 kB) View hashes Source None

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