Skip to main content

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

Project description

=======
django-generic-models
=======

.. image:: https://travis-ci.org/jpmelos/django-generic-models.svg?branch=master
:target: https://travis-ci.org/jpmelos/django-generic-models

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

Supports:

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

------------
Installation
------------

::

$ 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)
True

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``.

-----------
Development
-----------

Install for development and with dependencies::

$ pip install -e .[dev]

Asserting code quality
^^^^^^^^^^^^^^^^^^^^^^

::

$ python dev.py quality


Running the tests
^^^^^^^^^^^^^^^^^

To run the tests, use::

$ python dev.py test

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

$ tox

Packaging for PyPI distribution
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

::

$ python dev.py 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.

----------
Contribute
----------

Pull requests are welcome.


Project details


Release history Release notifications

This version
History Node

0.3

History Node

0.2

History Node

0.1.1

History Node

0.1

Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
django_generic_models-0.3-py3-none-any.whl (5.9 kB) Copy SHA256 hash SHA256 Wheel py3 Apr 23, 2018
django-generic-models-0.3.tar.gz (5.4 kB) Copy SHA256 hash SHA256 Source None Apr 23, 2018

Supported by

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