Skip to content

Commit 57608b2

Browse files
authored
Optionally specify apps (makecodes#3)
* Optionally get models from specified apps instead of all models * Support specifying app or app.Model matching dumpdata syntax * Briefly document the new option in the readme * Set version to 0.5
1 parent 9a026aa commit 57608b2

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ pip install django-dbml
2424
$ python manage.py dbml
2525
```
2626

27+
To generate DBML for a subset of your models, specify one or more Django app
28+
names or models by app_label or app_label.ModelName. Related tables will still
29+
be included in the DBML.
30+
2731
# Thanks
2832

2933
The initial code was based on https://github.com/hamedsj/DbmlForDjango project

django_dbml/management/commands/dbml.py

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
from django_dbml.utils import to_snake_case
22
from django.apps import apps
3-
from django.core.management.base import BaseCommand
3+
from django.core.management.base import BaseCommand, CommandError
44
from django.db import models
55

66

77
class Command(BaseCommand):
8-
help = "The main DBML management file"
8+
help = "Generate a DBML file based on Django models"
9+
10+
def add_arguments(self, parser):
11+
parser.add_argument(
12+
'args', metavar='app_label[.ModelName]', nargs='*',
13+
help='Restricts dbml generation to the specified app_label or app_label.ModelName.',
14+
)
915

1016
def get_field_notes(self, field):
1117
if len(field.keys()) == 1:
@@ -29,7 +35,35 @@ def get_field_notes(self, field):
2935
return ""
3036
return "[{}]".format(", ".join(attributes))
3137

32-
def handle(self, *args, **kwargs):
38+
def get_app_tables(self, app_labels):
39+
# get the list of models to generate DBML for
40+
41+
# if no apps are specified, process all models
42+
if not app_labels:
43+
return apps.get_models()
44+
45+
# get specific models when app or app.model is specified
46+
app_tables = []
47+
for app in app_labels:
48+
app_label_parts = app.split('.')
49+
# first part is always the app label
50+
app_label = app_label_parts[0]
51+
# use the second part as model label if set
52+
model_label = app_label_parts[1] if len(app_label_parts) > 1 else None
53+
try:
54+
app_config = apps.get_app_config(app_label)
55+
except LookupError as e:
56+
raise CommandError(str(e))
57+
58+
app_config = apps.get_app_config(app_label)
59+
if model_label:
60+
app_tables.append(app_config.get_model(model_label))
61+
else:
62+
app_tables.extend(app_config.get_models())
63+
64+
return app_tables
65+
66+
def handle(self, *app_labels, **kwargs):
3367
all_fields = {}
3468
allowed_types = ["ForeignKey", "ManyToManyField"]
3569
for field_type in models.__all__:
@@ -44,7 +78,8 @@ def handle(self, *args, **kwargs):
4478
)
4579

4680
tables = {}
47-
app_tables = apps.get_models()
81+
app_tables = self.get_app_tables(app_labels)
82+
4883
for app_table in app_tables:
4984
table_name = app_table.__name__
5085
tables[table_name] = {"fields": {}, "relations": []}

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = django-dbml
3-
version = 0.4.0
3+
version = 0.5.0
44
description = Django extension aimed to generate DBML from all models
55
long_description_content_type = text/markdown
66
long_description = file: README.md

0 commit comments

Comments
 (0)