3👍
import inspect
import test_module # module to be inspected
for name, data in inspect.getmembers(test_module):
if name.startswith("__"):
continue
if 'class' in str(data):
print(name)
0👍
One solution I just thought could be to make my own function that imports from .models and registers the elements, like
def import_and_register(arg_list):
for a in arg_list:
from .models import a
admin.site.register(a)
But can I do this with a builtin feature? Are there any major cons to this approach?
- [Django]-How to configure django to generate signed urls for media files with Google Cloud CDN?
- [Django]-How do I delete a collection in Django Rest Api?
- [Django]-Display item numbers with django paginator.
0👍
I don’t believe there is, but perhaps to save yourself from typing remember the names, just store them in a collection.
from .models import Food, Drink, Topping
MY_FANCY_MODELS = Food, Drink, Topping
for m in MY_FANCY_MODELS:
admin.site.register(m)
0👍
I feel, you can do it like this –
import models
for i in dir(models):
if isinstance(i, type):
admin.site.register(i)
dir(models) retrieves all the attributes of module.
- [Django]-Dynamically add to TEMPLATE_DIRS at runtime with a Django project
- [Django]-How to build complex django query as a string
- [Django]-Django rest api: JSON parse error – No JSON object could be decoded
- [Django]-Format numbers in Django with Intcomma
Source:stackexchange.com