On Thu, 2006-09-28 at 02:42 +0000, Gary Wilson wrote:
> Gacha wrote:
> > I have two model files, the first is:
> > -----------------------------------------------------------------------
> > from proj.base.models import Choice
> >
> > class User(meta.Model):
> >     ...
> >     param = model.ForeignKey(Choice)
> >
> > -----------------------------------------------------------------------
> >
> > and the second is :
> >
> > -----------------------------------------------------------------------
> > from proj.auth.models import User
> >
> > class Choice(meta.Model):
> >     ...
> >
> > class Discount(meta.model):
> >     ...
> >     user = model.ForeignKey(User)
> > -----------------------------------------------------------------------
> >
> > How you can see there are imports from each other, and there is the
> > problem, how I understand the class can't be buld before the imported
> > class is built.
> 
> Python does not allow circular imports, 

Somewhat unrelated, but that isn't completely correct. You just can't do
"from foo import bar" in a circular fashion. You can happily do "import
foo" from inside "bar" if foo also does "import bar, because it doesn't
try to resolve the contents until you access them.

Circular imports only fail because the entry is already in sys.modules,
but the module isn't fully initialised. And by running "import foo" you
only ensure that the name is in sys.modules: worrying about the contents
happens at access time and by then they will be fine.

All that being said, Django may have some interesting behaviour with
circular imports and ModelBase.__new__ calls and the app cache. I
haven't thought through what happens there.

> and using strings in the
> ForeignKey only works for model classes that are in the same python
> module (I think).  I think you might be stuck with having to move the
> model classes around.

Which kind of makes sense in a way: if the two models are that tightly
related that they have mutual references, they aren't really in
independent applications at that point.

Regards,
Malcolm



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to