Re: Problem with two applications importing models from each other?

2008-01-11 Thread Grindizer



On 10 jan, 22:00, Josh Ourisman <[EMAIL PROTECTED]> wrote:
> It's for the purpose of defining foreign keys. Basically, ClassA1
> needs to be able to reference ClassB1 and ClassB2 as foreign keys,
> while ClassB3 needs to reference ClassA1 as a foreign key.
>
> More specifically, the project has (among others) a news application
> and a portfolio application. An item of news might reference multiple
> items from the portfolio application, so we define a m2m relationship
> between the news item class and the appropriate portfolio classes
> (there are two that are relevant). At the same time, in the portfolio
> we want to be able to link to any relevant news items, but we _also_
> want to provide a way to prevent specific news items from showing up
> when looking at certain portfolio items. So it's not sufficient to
> just look at the news items related to a portfolio item through the
> existing m2m relationship, we also have to be able to define a
> separate list of news items.

Why you don't define the M2M relationship in the same model where the
Foreign Key is defined?
You will have dependence in only one direction: So no cross-reference
problems, and your applications will be more independents too ?

--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Re: Problem with two applications importing models from each other?

2008-01-10 Thread Josh Ourisman

Ok, that should help. Thanks very much.

On Jan 10, 4:58 pm, Rajesh Dhawan <[EMAIL PROTECTED]> wrote:
> On Jan 10, 4:00 pm, Josh Ourisman <[EMAIL PROTECTED]> wrote:
>
> > It's for the purpose of defining foreign keys. Basically, ClassA1
> > needs to be able to reference ClassB1 and ClassB2 as foreign keys,
> > while ClassB3 needs to reference ClassA1 as a foreign key.
>
> By now you know why this isn't working (models.py for neither app can
> be loaded by Python because of the circular references.)
>
> You could split your two models.py into multiple files. In particular,
> place each "cross referenced" model into its own file. That way, you
> will be able to import those cross referenced classes by name.
>
> See this:http://code.djangoproject.com/wiki/CookBookSplitModelsToFiles
>
> -Rajesh D
--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Re: Problem with two applications importing models from each other?

2008-01-10 Thread Rajesh Dhawan

On Jan 10, 4:00 pm, Josh Ourisman <[EMAIL PROTECTED]> wrote:
> It's for the purpose of defining foreign keys. Basically, ClassA1
> needs to be able to reference ClassB1 and ClassB2 as foreign keys,
> while ClassB3 needs to reference ClassA1 as a foreign key.

By now you know why this isn't working (models.py for neither app can
be loaded by Python because of the circular references.)

You could split your two models.py into multiple files. In particular,
place each "cross referenced" model into its own file. That way, you
will be able to import those cross referenced classes by name.

See this:
http://code.djangoproject.com/wiki/CookBookSplitModelsToFiles

-Rajesh D

--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Re: Problem with two applications importing models from each other?

2008-01-10 Thread Josh Ourisman

It's for the purpose of defining foreign keys. Basically, ClassA1
needs to be able to reference ClassB1 and ClassB2 as foreign keys,
while ClassB3 needs to reference ClassA1 as a foreign key.

More specifically, the project has (among others) a news application
and a portfolio application. An item of news might reference multiple
items from the portfolio application, so we define a m2m relationship
between the news item class and the appropriate portfolio classes
(there are two that are relevant). At the same time, in the portfolio
we want to be able to link to any relevant news items, but we _also_
want to provide a way to prevent specific news items from showing up
when looking at certain portfolio items. So it's not sufficient to
just look at the news items related to a portfolio item through the
existing m2m relationship, we also have to be able to define a
separate list of news items.

It's a little unnecessarily complex, I know, but it's what the client
wants and, if at all possible, I'd like to do it this way rather than
having to justify a change in functionality to the client.

On Jan 10, 3:19 pm, Rajesh Dhawan <[EMAIL PROTECTED]> wrote:
> Hi,
>
> On Jan 10, 12:54 pm, Josh Ourisman <[EMAIL PROTECTED]> wrote:
>
> > I have two models, modelA and modelB. ModelA needs classB1 and classB2
> > from modelB, while modelB needs classA1 from classA.
>
> Can you explain what you mean by "needs"? Is there a class method in
> modelA that needs to instantiate/call classB1? Better yet, include a
> short code snippet showing how ModelA needs classB1 and classB2.
>
> -Rajesh D
--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Re: Problem with two applications importing models from each other?

2008-01-10 Thread Rajesh Dhawan

Hi,

On Jan 10, 12:54 pm, Josh Ourisman <[EMAIL PROTECTED]> wrote:
> I have two models, modelA and modelB. ModelA needs classB1 and classB2
> from modelB, while modelB needs classA1 from classA.

Can you explain what you mean by "needs"? Is there a class method in
modelA that needs to instantiate/call classB1? Better yet, include a
short code snippet showing how ModelA needs classB1 and classB2.

-Rajesh D

--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Re: Problem with two applications importing models from each other?

2008-01-10 Thread Josh Ourisman

Nope, that just gets me "AttributeError: 'module' objects has no
attribute 'models'". :/

It doesn't work to just to 'import modelA' and then reference
modelA.Class1 or modelA.models.Class1 either; that just gives me the
same error.

On Jan 10, 2:38 pm, Muchanic <[EMAIL PROTECTED]> wrote:
> import xxx means 'import module xxx' - since Class1 is a class, and
> not a module, that won't work.
>
> If you change the import statements in both modules to something of
> the form 'import modelA.models as models_A' then referencing the
> classes as 'models_A.Class1', does that work?
>
> On Jan 11, 8:27 am, Josh Ourisman <[EMAIL PROTECTED]> wrote:
>
> > Hmm. That implies that I should be using 'import modelA.models.Class1'
> > rather than 'from modelA.models import Class1'. However, that syntax
> > doesn't seem to actually work. If I remove the circular imports for a
> > moment, 'from modelA.models import Class1' works just fine, but if I
> > use 'import modelA.models.Class1' I get a 'ImportError: No module
> > named Class1'.
>
> > I'm clearly missing _something_ here, and the advice given there
> > doesn't seem to work. Can anyone help me out?
>
> > On Jan 10, 1:40 pm, Muchanic <[EMAIL PROTECTED]> wrote:
>
> > > Check this 
> > > out:http://www.python.org/doc/faq/programming/#what-are-the-best-practice...
>
> > > On Jan 11, 6:54 am, Josh Ourisman <[EMAIL PROTECTED]> wrote:
>
> > > > I have two models, modelA and modelB. ModelA needs classB1 and classB2
> > > > from modelB, while modelB needs classA1 from classA.
>
> > > > So, in the models.py file for modelA I have 'from modelB.models import
> > > > classB1, classB2'. This worked just fine until I added to the
> > > > models.py file for modelB 'from modelA.models import classA1' after
> > > > also adding the class B3 that needed to reference classA1. Now when I
> > > > try to syncdb it tells me that modelA cannot import class B1.
>
> > > > Is there some particular reason that such cross-importing wouldn't be
> > > > possible? Do I need to use a different importing syntax or something?- 
> > > > Hide quoted text -
>
> > - Show quoted text -
--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Re: Problem with two applications importing models from each other?

2008-01-10 Thread Muchanic

import xxx means 'import module xxx' - since Class1 is a class, and
not a module, that won't work.

If you change the import statements in both modules to something of
the form 'import modelA.models as models_A' then referencing the
classes as 'models_A.Class1', does that work?

On Jan 11, 8:27 am, Josh Ourisman <[EMAIL PROTECTED]> wrote:
> Hmm. That implies that I should be using 'import modelA.models.Class1'
> rather than 'from modelA.models import Class1'. However, that syntax
> doesn't seem to actually work. If I remove the circular imports for a
> moment, 'from modelA.models import Class1' works just fine, but if I
> use 'import modelA.models.Class1' I get a 'ImportError: No module
> named Class1'.
>
> I'm clearly missing _something_ here, and the advice given there
> doesn't seem to work. Can anyone help me out?
>
> On Jan 10, 1:40 pm, Muchanic <[EMAIL PROTECTED]> wrote:
>
>
>
> > Check this 
> > out:http://www.python.org/doc/faq/programming/#what-are-the-best-practice...
>
> > On Jan 11, 6:54 am, Josh Ourisman <[EMAIL PROTECTED]> wrote:
>
> > > I have two models, modelA and modelB. ModelA needs classB1 and classB2
> > > from modelB, while modelB needs classA1 from classA.
>
> > > So, in the models.py file for modelA I have 'from modelB.models import
> > > classB1, classB2'. This worked just fine until I added to the
> > > models.py file for modelB 'from modelA.models import classA1' after
> > > also adding the class B3 that needed to reference classA1. Now when I
> > > try to syncdb it tells me that modelA cannot import class B1.
>
> > > Is there some particular reason that such cross-importing wouldn't be
> > > possible? Do I need to use a different importing syntax or something?- 
> > > Hide quoted text -
>
> - Show quoted text -
--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Re: Problem with two applications importing models from each other?

2008-01-10 Thread Josh Ourisman

Hmm. That implies that I should be using 'import modelA.models.Class1'
rather than 'from modelA.models import Class1'. However, that syntax
doesn't seem to actually work. If I remove the circular imports for a
moment, 'from modelA.models import Class1' works just fine, but if I
use 'import modelA.models.Class1' I get a 'ImportError: No module
named Class1'.

I'm clearly missing _something_ here, and the advice given there
doesn't seem to work. Can anyone help me out?

On Jan 10, 1:40 pm, Muchanic <[EMAIL PROTECTED]> wrote:
> Check this 
> out:http://www.python.org/doc/faq/programming/#what-are-the-best-practice...
>
> On Jan 11, 6:54 am, Josh Ourisman <[EMAIL PROTECTED]> wrote:
>
> > I have two models, modelA and modelB. ModelA needs classB1 and classB2
> > from modelB, while modelB needs classA1 from classA.
>
> > So, in the models.py file for modelA I have 'from modelB.models import
> > classB1, classB2'. This worked just fine until I added to the
> > models.py file for modelB 'from modelA.models import classA1' after
> > also adding the class B3 that needed to reference classA1. Now when I
> > try to syncdb it tells me that modelA cannot import class B1.
>
> > Is there some particular reason that such cross-importing wouldn't be
> > possible? Do I need to use a different importing syntax or something?
--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Re: Problem with two applications importing models from each other?

2008-01-10 Thread Muchanic

Check this out: 
http://www.python.org/doc/faq/programming/#what-are-the-best-practices-for-using-import-in-a-module

On Jan 11, 6:54 am, Josh Ourisman <[EMAIL PROTECTED]> wrote:
> I have two models, modelA and modelB. ModelA needs classB1 and classB2
> from modelB, while modelB needs classA1 from classA.
>
> So, in the models.py file for modelA I have 'from modelB.models import
> classB1, classB2'. This worked just fine until I added to the
> models.py file for modelB 'from modelA.models import classA1' after
> also adding the class B3 that needed to reference classA1. Now when I
> try to syncdb it tells me that modelA cannot import class B1.
>
> Is there some particular reason that such cross-importing wouldn't be
> possible? Do I need to use a different importing syntax or something?
--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Problem with two applications importing models from each other?

2008-01-10 Thread Josh Ourisman

I have two models, modelA and modelB. ModelA needs classB1 and classB2
from modelB, while modelB needs classA1 from classA.

So, in the models.py file for modelA I have 'from modelB.models import
classB1, classB2'. This worked just fine until I added to the
models.py file for modelB 'from modelA.models import classA1' after
also adding the class B3 that needed to reference classA1. Now when I
try to syncdb it tells me that modelA cannot import class B1.

Is there some particular reason that such cross-importing wouldn't be
possible? Do I need to use a different importing syntax or something?
--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Re: Importing models from each other.

2006-10-04 Thread Holger Schurig

> I try to import like: "import myproject" and then
> myproject.auth.models.User but no luck, Django throws error,
> that auth has no models class. Is there a way to make this to
> work?

For this kind of problem it helps sometimes to use the "-v" 
option to python, e.g.

python -v ./manage.py runserver

and then look what modules it wants to load.


Maybe it's also as simple as a missing empty __init__.py in the 
myproject/auth directory.

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Importing models from each other.

2006-10-03 Thread Gacha

I separated the models. Now I have myproj.extra which contains the
Choice model and all works fine :)


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Importing models from each other.

2006-10-02 Thread Malcolm Tredinnick

On Mon, 2006-10-02 at 06:41 -0700, Gacha wrote:
> I try to import like: "import myproject" and then
> myproject.auth.models.User but no luck, Django throws error, that auth
> has no models class. Is there a way to make this to work?

I don't really have any ideas of the top of my head. However, for
tightly linked, mutually-referential models like you are creating, I
think the simplest is going to be to put them in the one file.

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
-~--~~~~--~~--~--~---



Re: Importing models from each other.

2006-10-02 Thread Gacha

I try to import like: "import myproject" and then
myproject.auth.models.User but no luck, Django throws error, that auth
has no models class. Is there a way to make this to work?


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Importing models from each other.

2006-09-27 Thread Malcolm Tredinnick

On Thu, 2006-09-28 at 02:42 +, 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
-~--~~~~--~~--~--~---



Re: Importing models from each other.

2006-09-27 Thread Gacha

Nope, it didn't work :( but thx for a try


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Importing models from each other.

2006-09-27 Thread Steven Armstrong

On 09/27/06 16:03, 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.
> 
> One of the workaournds is to put all classes in one file, but I don't
> want to do that, because auth module is for the user stuff and base
> module is for the rest classes.
> 
> maybe someone has any ideas. (sorry, for bad english)
> 

Not sure about this, but maybe it works if you change the order of the 
imports and the class definition?

e.g.

class Choice(meta.Model):
 ...

from proj.auth.models import User
class Discount(meta.model):
 ...
 user = model.ForeignKey(User)


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Importing models from each other.

2006-09-27 Thread Gacha

Yes, I already know that :)


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Importing models from each other.

2006-09-27 Thread [EMAIL PROTECTED]

[EMAIL PROTECTED] wrote:

> You need to pass the model name as a string:
> user = model.ForeignKey("User")

Actually i'm seeing you knew that already, yes to use the technique
above you need to have all models in the same file.

Lorenzo


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Importing models from each other.

2006-09-27 Thread [EMAIL PROTECTED]


Gacha wrote:
> user = model.ForeignKey(User)

You need to pass the model name as a string:
user = model.ForeignKey("User")

Lorenzo


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Importing models from each other.

2006-09-27 Thread Gacha

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.

One of the workaournds is to put all classes in one file, but I don't
want to do that, because auth module is for the user stuff and base
module is for the rest classes.

maybe someone has any ideas. (sorry, for bad english)


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---