0

I am pretty new to Django.

I wanted to create a form for some user information. Depending on type of user informations, the fields should change... For example the users private address needs the fields name, street, zip and city. But if he wants something send to the company, there might be more fields like department or company name.

I want to implement something like this and create for each kind of input an extra model compact in a separate app.

Is there a way to get a select field with a list of all available models in this app.


Edit

Since I have some further problems, I add an example here

file: experiment/models.py

from django.db import models
from django.apps import apps
class BasicExperiment(models.Model):
    date_created = models.DateTimeField(editable=False)
    date_modified = models.DateTimeField(blank=True)
    label_app = apps.get_app('labels')
    label_types = apps.get_models(label_app)

file: labels/models.py

from django.db import models
class SILAC(models.Model):
    lys0 = models.BooleanField('Lys-0', default=True)
    lys4 = models.BooleanField('Lys-4', default=None)
    lys8 = models.BooleanField('Lys-8', default=None)
    arg0 = models.BooleanField('Arg-0', default=True)
    arg6 = models.BooleanField('Arg-6', default=None)
    arg10 = models.BooleanField('Arg-10', default=None)
    class Meta:
        verbose_name = 'SILAC Labeling'

In the shell it works as expected:

>>> from django.apps import apps
>>> app = apps.get_app('labels')
>>> for model in apps.get_models(app):
...     model._meta.verbose_name
... 
'SILAC Labeling'

Within my models.py I get the following error:

...
  File "/Users/madejung/Documents/django_dev/cfproteomics/experiments/models.py", line 5, in <module>
    class BasicExperiment(models.Model):
  File "/Users/madejung/Documents/django_dev/cfproteomics/experiments/models.py", line 10, in BasicExperiment
    label_app = apps.get_app('labels')
  File "/Library/Python/2.7/site-packages/django/apps/registry.py", line 370, in get_app
    "App '%s' doesn't have a models module." % app_label)
django.core.exceptions.ImproperlyConfigured: App 'labels' doesn't have a models module.
halfer
  • 19,824
  • 17
  • 99
  • 186
drmariod
  • 11,106
  • 16
  • 64
  • 110
  • Do you mean to obtain all the fields a model has defined? – lapinkoira Mar 25 '15 at 14:09
  • No, not really. I know how to get the all fields from ModelA or from ModelB... What I need is kind of drop down menu to choose any model Model(ABCDE...) in my app... – drmariod Mar 25 '15 at 14:13
  • Check out these questions: http://stackoverflow.com/questions/1125107/django-how-can-i-find-a-list-of-models-that-the-orm-knows http://stackoverflow.com/questions/8702772/django-get-list-of-models-in-application – azalea Mar 25 '15 at 14:17

1 Answers1

1

You could try this:

from django.db.models import get_app, get_models

app = get_app('my_application_name')
for model in get_models(app):
    # do something with the model

Here there is more information Django get list of models in application

Community
  • 1
  • 1
lapinkoira
  • 8,320
  • 9
  • 51
  • 94
  • I am testing you code at the moment and it is working fine in the shell... But if I copy paste it into my model from a different app, I get an error message `django.core.exceptions.ImproperlyConfigured: App 'labels' doesn't have a models module.` So I can not use `get_app` in my module, or maybe I have to kind of set a relative path? – drmariod Mar 26 '15 at 09:32
  • mm Could you write here the structure of your project and the app label? – lapinkoira Mar 26 '15 at 09:33
  • I maybe just figured out that [contenttypes](https://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/) are what I was looking for... Since I want to have kind of drop down menu which model I will create... – drmariod Mar 26 '15 at 14:20