10👍
✅
You can do it in the python console, or in *.py file:
from django.core.management.commands.inspectdb import Command
from django.conf import settings
from your_project_dir.settings import DATABASES # replace `your_project_dir`
settings.configure()
settings.DATABASES = DATABASES
Command().execute(table_name_filter=lambda table_name: table_name in ('table_what_you_need_1', 'table_what_you_need_2', ), database='default')
https://github.com/django/django/blob/master/django/core/management/commands/inspectdb.py#L32
20👍
You can generate the model of a single table, running this command
python manage.py inspectdb TableName > output.py
This works also if you want to generate the model of a view
- What's the best way to use CoffeeScript with Django if you're developing on Windows?
- Regex django url
- Django test client response context None
- Validation on query_params in Django Rest Framework
- Django REST framework: Can a nested object access its parent object's details in a List View?
4👍
You can do it by the following command in Django 2.2 or above
python manage.py inspectdb --database=[dbname] [table_name] > output.py
1👍
You can get the models of the tables you want by doing:
python manage.py inspectdb table1 table2 tableN > output.py
This way you can select only the tables you want.
- Django 'resolve' : get the url name instead of the view_function
- Whats the best way to extend Anonymous User in Django?
- How can I use OrderingFilter without exposing the names of the fields in the database
- Django/Python Runtime Error: Maximum recursion depth exceeded
- In Django, how do you retrieve a field of a many-to-many related class?
0👍
You can generate model’s python code and write to the console programmatically.
from django.core.management.commands.inspectdb import Command
command = Command()
command.execute(
database='default',
force_color=True,
no_color=False,
include_partitions=True,
include_views=True,
table=[
'auth_group',
'django_session'
]
)
set table=[]
empty list to get all tables
- How to obtain a plain text Django error page
- Django how to make form fields optional
- Django queryset union appears not to be working when combined with .annotate()
- Indirect inline in Django admin
- Best Practice for Context Processors vs. Template Tags?
Source:stackexchange.com