splitting models.py

32 views
Skip to first unread message

Mike Dewhirst

unread,
Aug 8, 2012, 3:27:36 AM8/8/12
to django...@googlegroups.com
I would like to understand the "formula" for splitting a monolithic
models.py into ./models/<classname>.py files.

Can someone point to somewhere in the docs or source which sets out the
logic?

I have three apps in a project and one has a models.py with 18 models
and various 1:n and n:m relationships with data-carrying "through" tables.

I wrote a script to split everything up and add the appropriate import
lines to each models .py file. That worked but only after quoting some
model names in relationships and specifically importing unquoted
modelnames in others.

I would love to improve that script so it will work for the other apps.

I'll contribute it back if I get it working.

Thanks for any help

Cheers

Mike

Russell Keith-Magee

unread,
Aug 8, 2012, 4:10:48 AM8/8/12
to django...@googlegroups.com
On Wed, Aug 8, 2012 at 3:27 PM, Mike Dewhirst <mi...@dewhirst.com.au> wrote:
> I would like to understand the "formula" for splitting a monolithic
> models.py into ./models/<classname>.py files.
>
> Can someone point to somewhere in the docs or source which sets out the
> logic?

There isn't really any "logic" to set out -- you just need to add the
"app_label" meta argument to any model that isn't in a file named
models.py.

https://docs.djangoproject.com/en/dev/ref/models/options/#app-label

If you want some examples of the technique in action, Django's test
suite contains an example in the "tests/modeltests/model_package"
directory.

Yours,
Russ Magee %-)

Mike Dewhirst

unread,
Aug 8, 2012, 6:43:25 PM8/8/12
to django...@googlegroups.com
On 8/08/2012 6:10pm, Russell Keith-Magee wrote:
> On Wed, Aug 8, 2012 at 3:27 PM, Mike Dewhirst <mi...@dewhirst.com.au> wrote:
>> I would like to understand the "formula" for splitting a monolithic
>> models.py into ./models/<classname>.py files.
>>
>> Can someone point to somewhere in the docs or source which sets out the
>> logic?
>
> There isn't really any "logic" to set out -- you just need to add the
> "app_label" meta argument to any model that isn't in a file named
> models.py.
>

Yes, that part is fine ... see below.

> https://docs.djangoproject.com/en/dev/ref/models/options/#app-label
>
> If you want some examples of the technique in action, Django's test
> suite contains an example in the "tests/modeltests/model_package"
> directory.

Ok. The bits I will now try in __init__.py are absolute imports prefixed
by a dot from the future and actually adding the <app-prefix>.modelname
when specifying the related model.

I'm still a little behind but some experimentation may sort it out. I
think I'm in a circular import swirl and need to figure out exactly what
is happening.

Thanks

Mike

>
> Yours,
> Russ Magee %-)
>

Reply all
Reply to author
Forward
0 new messages