The snippet above: http://djangosnippets.org/snippets/1838/
did have some incantation but the post was fairly old in terms of django/python's history.
I can't tell one incantation from another but I can tell a django app that fails to sync from one that does.
If anybody has trouble with this kind of django project structure,
Mike's incantation works just right, I don't want to be as bold as to say the one in the snippet
doesn't work, but I couldn't get it to work.
if its not already clear heres my set up:
#my_project
# manage.py# settings.py
# myapp/
# models/ # __init__.py # models1.py # models2.py
------------------------------------------------------
in __init__.py:
in __init__.py:
# incantation
from __future__ import absolute_import
from .models1 import ModelOne from .models2 import ModelTwo
------------------------------------------------------
-- in models1.py:
from myapp.models import Model2
------------------------------------------------------
On Thursday, May 30, 2013 8:43:46 AM UTC-4, Doug S wrote:
------------------------------------------------------
in models2.py:
from myapp.models import Model1
------------------------------------------------------
in admin.py:
from my_app.models import Model1, Model2
I also took a precaution that I'm not sure was required:
I made sure Django knew which app to put all my Model1 & Model2 in
by defining a Meta property like this:
class Meta:
app_label = 'myapp'
On Thursday, May 30, 2013 8:43:46 AM UTC-4, Doug S wrote:
I'm trying to use multiple files for my models in a single app.I found a nice solution on Django Snippets:http://djangosnippets.org/snippets/1838/ but I'm not able to get it to work.The docs assume that I know more than I do about how this solution works.Its involves using a models folder for the app like this:# myapp/ # models/ # __init__.py # models1.py # models2.py
and then including an __init__.py file in the folder to make it a package
this file also contains this code:
I don't understand this code, but hopefully I don't have to.
import django.db.models import sys appname = "myapp" from models1 import * from models2 import * __all__ = [] for decl in globals().values(): try: if decl.__module__.startswith(__name__ ) and issubclass(decl, django.db.models.Model): decl._meta.db_table = decl._meta.db_table.replace('models' , appname) decl._meta.app_label = appname __all__.append(decl.__name__) django.db.models.loading.register_models (appname, decl) except: passI just want to be able to import my individual model modules:models1.py & models2.pyfrom my views and admin.pythe import statements are not included in the snippetand I've tried what seems reasonable and keep getting errors importing the modules saying they don't existI would expect you do something likeimport myapp.models.model1&import myapp.models.model2but this doesn't work.maybe I don't understand the magic in the __init__.py fileor I'm just being plain stupid.Can someone point me toward getting this solution working with my imports?
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
No comments:
Post a Comment