diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..73a2e68 --- /dev/null +++ b/README.rst @@ -0,0 +1,46 @@ +==================== +django-database-view +==================== + +A simple pluggable application that allows to work with database views. + +Quick start +----------- + +1. Add "dbview" to your INSTALLED_APPS settings like this:: + + INSTALLED_APPS = ( + ... + 'dbview', + ) + +2. In your models.py create classes which extend dbview.models.DbView +like this:: + +.. code-block:: python + :caption: models.py + :name: models.py + + ... + from dbview.models import DbView + + ... + class MyView(DbView): + fieldA = models.OneToOneField(modelA, primary_key=True, db_column='fielda__id') + fieldB = models.IntegerField(blank=True, null=True, db_column='fieldb') + + @classmethod + dev view(klass): + ''' + This method returns the SQL string that creates the view, in this + example fieldB is the result of annotating another column + ''' + qs = modelA.objects.all().\ + annotate(fieldb=models.Sum('fieldc')) .\ + annotate(fielda__id=models.F('pk')) .\ + order_by('fielda__id') .\ + values('fielda__id', 'fieldb') + return str(qs.query) + +3. Then create a migration point for your view generation, edit that migration +and modify it to match: diff --git a/dbview/__init__.py b/dbview/__init__.py new file mode 100644 index 0000000..d1cf9b7 --- /dev/null +++ b/dbview/__init__.py @@ -0,0 +1 @@ +# placeholder for git diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..e517b0b --- /dev/null +++ b/setup.py @@ -0,0 +1,37 @@ +import os +from setuptools import setup + +with open(os.path.join(os.path.dirname(__file__), 'README.rst')) as readme: + README = readme.read() + +# allow setup.py to be run from any path +os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir))) + +setup( + name='django-database-view', + version='0.1', + packages=['dbview'], + include_package_data=True, + license='MIT', + description='A simple Django app to handle database views.', + long_description=README, + url='https://github.com/manuelnaranjo/django-database-view', + author='Manuel F. Naranjo', + author_email='naranjo.manuel@gmail.com', + classifiers=[ + 'Environment :: Web Environment', + 'Framework :: Django', + 'Intended Audience :: Developers', + 'License :: OSI Approved :: MIT License', + 'Operating System :: OS Independent', + 'Programming Language :: Python', + 'Programming Language :: Python :: 2', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.2', + 'Programming Language :: Python :: 3.3', + 'Programming Language :: Python :: 3.4', + 'Topic :: Internet :: WWW/HTTP', + 'Topic :: Internet :: WWW/HTTP :: Dynamic Content', + ], +)