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

霸气de小男生 提交于 2021-01-27 07:03:24

问题


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:

  • Google Search "generate django app from db schema"
  • Reviewed all the documentation referenced in How to create a sample django project?
  • Reviewed the list of available cookiecutters at https://github.com/audreyr/cookiecutter

回答1:


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.




回答2:


As Dan pointed out we can use inspectdb to generate models files, once we have models.py files we can integrate that to admin.py manual or can be done by adding all of them; we need to update urls.py to include admin generated views.

This won't be much useful unless we've some custom views, you can add data in models and view them in the admin panel. Admin view supports pagination as well. There're many more features in the admin view that's like traditional python class with added methods and fields.



来源:https://stackoverflow.com/questions/52410615/how-to-auto-generate-a-sample-django-application-from-a-database-schema

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!