Re: Clarifications for apps and models

2009-04-07 Thread Malcolm Tredinnick

On Tue, 2009-04-07 at 18:29 -0700, Thierry wrote:
> I'll be referring to the example in the Django documentation here.  I
> will extend on the "books" application in the "mysite" project by
> creating a new app called "music" so that the structure looks like the
> following:
> 
> mysite/
> books/
> models.py - Contains Publisher, Author, Book
> music/
> models.py - Contains Artist, Album
> 
> Let's also assume that the Publisher model from books also publish
> music.  How can I can refer to Publisher from music/models.py?   Is
> there a place under mysite where I can have a common models.py used by
> all my apps?  For example, is mysite/models.py a viable option?

Django models are, for the most part, simply Python classes. So if you
want to refer to a model (class) from somewhere, else, simply import it
and use it.

Thus, in music/models.py:

from books import models as book_models

...
# Refer to Publisher like this
book_models.Publisher

You could even directly import Publisher into the namespace (from
books.models import Publisher). My preference is for the PEP 8 style of
importing the module (helps avoid circular import problems, too), but
that's clearly not everybody's way.

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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Clarifications for apps and models

2009-04-07 Thread Thierry

I'll be referring to the example in the Django documentation here.  I
will extend on the "books" application in the "mysite" project by
creating a new app called "music" so that the structure looks like the
following:

mysite/
books/
models.py - Contains Publisher, Author, Book
music/
models.py - Contains Artist, Album

Let's also assume that the Publisher model from books also publish
music.  How can I can refer to Publisher from music/models.py?   Is
there a place under mysite where I can have a common models.py used by
all my apps?  For example, is mysite/models.py a viable option?

--~--~-~--~~~---~--~~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---