Django get_models con modelos/__init.py__

I'm having a problem using get_model and get_models in django

I have several models under models/

 models/blog.py
 models/tags.py
 models/users.py
 models/comments.py
 models/category.py

Y un models/__init.py__

from myapp.models.blog import *
from myapp.models.tags import *
from myapp.models.users import *
from myapp.models.comments import *   
from myapp.models.category import *

However in my views.py I have some code to use get_model

from django.db.models.loading import get_model
blog_class = get_model('myapp', 'blog') #Returns none

Cuando lo intento get_models('myapp') devuelve una lista vacía.

También probé

print(get_app('myapp'))

Que devuelve:

<module 'myapp.models' from '/var/www/myapp/models/__init__.pyc'> 

And if I try to iterate over it

for model in get_models(get_app('myapp')):
    print(model)

It does nothing. Is there anything I'm missing or failing to spot?

preguntado el 31 de julio de 12 a las 12:07

1 Respuestas

Because you haven't defined your models in the app's models.py, you must explicitly set the app_label option for each model.

class Blog(models.Model):
    title = models.CharField(max_length=30)
    ...

    class Meta:
        app_label = 'myapp'

Respondido 31 Jul 12, 12:07

Excellent, thanks very much. It turned out to be a case issue between Myapp and myapp. - Shane

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.