On Apr 20, 2:57 am, x_O <sebastian.paw...@gmail.com> wrote:
> Hi
>
> I'm looking for some general solution how to split models.py. Right
> now my models.py reached 1000 lines and that argues some how with good
> programing behaviour that I used use.
>
> I'm thinking about:
> myproject/
>    settings.py
>    myapp/
>       models/
>          file.py
>          directory.py
>
> instead of classic solution:
> myproject/
>    settings.py
>    myapp/
>       models.py
>       views.py
>
> to In the simple case where in models we are able to avoid cyclic
> import there is no problem. But what when classes definition use each
> other implementation.
> Django implements very nice and tricky solution using 'string' to
> import definition in related 
> fields.http://docs.djangoproject.com/en/dev/ref/models/fields/#module-django...
> but in current case its useless.
>
> Any idea how to win with that problem?
>
> x_O

Hi,

You're actually dealing with two separate issues:

 1) How to move the model files into a separate package (i.e., a
directory named "models/").
 2) How to reference models in different modules, that may or may not
have been defined.

To move the models into their own packages, move them into a directory
named "models/", and create a file name __init__.py to make the
directory a package:

myapp/
  models/
    __init__.py
    one.py
    two.py

In one.py, define your first model, but you'll need to add the
attribute "app_label" to the Meta inner class. Here's one.py:

from django.db import models

class ModelOne(models.Model):
    class Meta:
        app_label = 'myapp'

Now, in two.py, you can either reference ModelOne using its name, or
by importing it first. Here's the string version of two.py:

from django.db import models

class ModelTwo(models.Model):
    one = models.ForeignKey('ModelOne')

    class Meta:
        app_label = 'myapp'

Here's the import version of two.py:

from django.db import models
from one import ModelOne

class ModelTwo(models.Model):
    one = models.ForeignKey(ModelOne)

    class Meta:
        app_label = 'myapp'

Finally, you're going to need to import these models into the
__init__.py modules, so that when it's imported later, it finds your
models. Here's the contents of __init__.py:

from one import ModelOne
from two import ModelTwo

A way to check that all of this is working is to either use "manage.py
validate", or "manage.py sql myapp", to view the SQL that will be used
to created the models' tables. Here's what I see:

rpwagner$ ./manage.py validate
0 errors found
rpwagner$ ./manage.py sql myapp
BEGIN;
CREATE TABLE "myapp_modelone" (
    "id" integer NOT NULL PRIMARY KEY
)
;
CREATE TABLE "myapp_modeltwo" (
    "id" integer NOT NULL PRIMARY KEY,
    "one_id" integer NOT NULL REFERENCES "myapp_modelone" ("id")
)
;
COMMIT;
rpwagner$

Hope this helps.

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

Reply via email to