So, i have a file models.py in MyApp folder:
from django.db import models
class Model_One(models.Model):
...
class Model_Two(models.Model):
...
...
It can be about 10-15 classes. How to find all models in the MyApp and get their names?
Since models are not iterable, I don't know if this is even possible.
Here's a quick-and-dirty, no-coding solution using
dumpdata
andjq
:You could also clean up the
jq
command to get the format just to your liking.Bonus: you can see counts of the different types of objects by adding the
-c
flag touniq
.An alternative is to use Content Types.
Each models for each application in INSTALLED_APPS get an entry in the ContentType models. This allow you, for exemple, to have a foreign key to a model.
From Django 1.7 on, you can use this code, for example in your admin.py to register all models:
UPDATE
for newer versions of Django check Sjoerd answer below
Original answer from 2012: This is the best way to accomplish what you want to do:
In this example,
model
is the actual model, so you can do plenty of things with it:Best answer I found to get all models from an app: