Tuesday, November 30, 2010

Re: Issues importing models in between apps

heya,

Thanks to both for your advice.

Yeah, you're right, it's a somewhat unwanted circular dependency. It
was just the only way I could think of to deal with a somewhat
unwieldy/large models.py file.

Even using the string syntax, I hit a bunch of other issues, due to
how tightly the two models are coupled ('Accessor clashes with related
field...', the 'through' table for m2m needs to be imported etc.). So
it might be better to try and sort out the design issue first.

To give some context - it's a simple application for managing people's
attendance at conferences, and allocating accomodation to them.

* I have a 'People' app that has a models 'Person', along with models
for all their required information (allergies, special requirements,
spouse/children, contact numbers and addresses etc.).

* I have a 'Conferences' app that imports Person, and stores things
about their attendance, accommodation requests, what workshop they're
assigned to etc.

* I have a 'Facilities' app that manages all the various accomodation
sites and workshop rooms, and what they each provide. (e.g. how many
beds, how many seats etc.)

There's a few others, but those are the main ones.

Previously, I had this all in a single models.py inside Conferences.
However, it got a bit unwieldy, hence the split. However, they're all
still tightly coupled.

With the above information, would you still recommend putting it all
in a single app - or should I try and work out the issues from this
split? It did seem cleaner in separate apps.

Anyhow, from my understanding, I can split up the models.py file

http://code.djangoproject.com/ticket/4470
http://andrewwilkinson.wordpress.com/2009/01/20/placing-django-models-in-separate-files/

However, I'll need to add a Meta/app_label to each and every single
model in that file - is that right? Or is there a better way of
splitting up a models.py file across multiple files, or of somehow
organising this?

Cheers,
Victor

On Nov 30, 10:44 pm, bruno desthuilliers
<bruno.desthuilli...@gmail.com> wrote:
> On 30 nov, 07:01, Victor Hooi <victorh...@gmail.com> wrote:
>
> > Hi,
>
> > I had an application with a fairly large models.py file, which I split
> > up into separate apps to make it easier to manage.
>
> This won't address your problem but FWIW, you don't have to use
> separate apps just to split a models.py file - you can turn it into a
> models package, with the package's __init__.py used as a "facade" for
> the submodules (usual pythonic solution).
>
> But anyway:
>
> > Two of the apps are called "conferences" and "people", each with their
> > own models.py.
>
> > These two are fairly tightly entwined, they each refer to each other.
>
> If both models really depend that much on each other then they ought
> te live in a same module. Or perhaps there's room for improvement in
> your design but you don't give enough context.
>
> (snip typical circular deps symptom)
>
> > I'm not sure why this is happening - from the looks of it, it's
> > opening up conferences/models.py, then importing people.models.Person
> > from there, which inside of that tries to import Conference
>
> In Python almost everything happens at runtime. The import, class and
> def statements are executable statements, and they are executed - with
> every other top-level statement - when the module is first loaded.
>
> > Or is there a better way of creating multiple apps that all share
> > models?
>
> Put the interdependent models in a same app. The you can have other
> apps using these models too as long as there's no circular
> dependencies.
>
> A Django app doesn't have to define models or views or urls or
> whatever - you can have one app with models, and another vwith views
> and forms and whatnot depending on the first app's models.
>
> > The main reason I split it up was for logically separating all
> > the different models, they still all need to import/relate to each
> > other.
>
> Once again: if your models are tightly coupled, they ought to live in
> a same module.

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

No comments:

Post a Comment