Search code examples
pythondjangocookiecutter-django

How to auto-generate a sample Django application from a database schema?


I am evaluating frameworks for a Proof Of Concept application. This application will have a life-cycle of about 30 days, after which it will be either forgotten or entirely rewritten. I have determined that I want to auto-generate a sample app from existing database schemas, and then just tweak some aspects of the visual design. I have watched a demo of this being done on Ruby on Rails: it auto-generates a simple view and data entry form for each table in the database, with built-in pagination etc. I've also already determined that I need to use Python, not Ruby, for this project.

So I came upon this question:

Python on Rails?

The answers there referred me to Django.

So the question. How can I auto-generate a simple CRUD application from database schemas, using Django, similar to what can be done on RoR?

What I have tried so far:


Solution

  • Use Django's inspectdb to generate your models and then use the Django Admin to enter/manage data.

    You would first create a Django project and run inspectdb to create the models. You would then create an app within the project, and move the models file you created over to replace the default models.py file in the app. Ensure the Django admin is enabled. Then, within the app, you would add to the default admin.py file the models to include in the admin. You can get a quick look at the admin by just including admin.site.register(ModelName) in admin.py for each model you want to enter data into. To tailor the presentation of the model, you just create a class YourModelAdmin(admin.ModelAdmin) and define how you want a particular model to appear.

    Setting up a basic admin site is actually very quick and one of the strong points of Django.