Possible to create models programatically?

491 views
Skip to first unread message

Andy

unread,
Jul 20, 2010, 8:56:11 PM7/20/10
to Django users
I have N models that are identical except the model names (i.e. they
all have the same attributes, methods):

MyModel0, MyModel1, MyModel2, ...

Normally I'd need to type out the model definitions for those N models
by hand inside models.py.

Is there a way to generate the N model definitions programatically
instead of manually?

Doug

unread,
Jul 20, 2010, 9:25:30 PM7/20/10
to Django users
I have a similar issue, with a bunch of very similar apps that I need
in separate tables that all have the same base structure. I keep the
common models in a file called models_base.py and then use execfile in
the models.py file for each app. It's basically like a php or c
include in that in 'inserts' in the local. You can also make changes/
add fields to a model after it's defined by placing the assignments
after the execfile.

execfile('somefilenameandpath.py',locals(),globals())

Having said that, I don't think it is generally a good idea to do
this. It seems much better now to use subclassing with tables
specified, or some other approach. In my case, the project was built
well before model subclassing was implemented. Otherwise we woud have
used that. It does work though if you have to do it that way.

Ale

unread,
Jul 20, 2010, 9:43:34 PM7/20/10
to django...@googlegroups.com
On Tue, Jul 20, 2010 at 9:56 PM, Andy <selfor...@gmail.com> wrote:
Normally I'd need to type out the model definitions for those N models
by hand inside models.py.

Is there a way to generate the N model definitions programatically
instead of manually?


Did you try creating an abstract model, and have the rest of the N models inherit form the abstract models, you save yourself some typing  http://docs.djangoproject.com/en/dev/topics/db/models/#id6

Or that's not good enough?

--
Ale.

Daniel Roseman

unread,
Jul 21, 2010, 9:24:01 AM7/21/10
to Django users
I'm not completely sure what you're trying to do, but I have a blog
entry on creating temporary models programmatically - it might help
you:
http://blog.roseman.org.uk/2010/04/13/temporary-models-django/
--
DR.

Andy

unread,
Jul 22, 2010, 4:06:05 AM7/22/10
to Django users
>
> I'm not completely sure what you're trying to do, but I have a blog
> entry on creating temporary models programmatically - it might help
> you:http://blog.roseman.org.uk/2010/04/13/temporary-models-django/

What I'm trying to do is to shard my database horizontally.

For example say I have a model Tweet and I want to shard it into N
tables using the modulo N of the user.id

So now I have N different models of Tweet: Tweet0, Tweet1, Tweet2, ...
each has the same structure, differing only in names.

I could type out the N models by hand:

class Tweet0(models.Model):
content = models.CharField(max_length=140)
datetime = models.DateTimeField()
...

class Tweet1(models.Model):
content = models.CharField(max_length=140)
datetime = models.DateTimeField()
...

class Tweet2(models.Model):
content = models.CharField(max_length=140)
datetime = models.DateTimeField()
...

And I'd have to repeat that N times.

Just wanted to see if there's a way to generate those N models
programmatically (perhaps using a for loop) instead of typing them out
manually.

Russell Keith-Magee

unread,
Jul 22, 2010, 6:46:30 AM7/22/10
to django...@googlegroups.com
On Thu, Jul 22, 2010 at 4:06 PM, Andy <selfor...@gmail.com> wrote:
>>
>> I'm not completely sure what you're trying to do, but I have a blog
>> entry on creating temporary models programmatically - it might help
>> you:http://blog.roseman.org.uk/2010/04/13/temporary-models-django/
>
> What I'm trying to do is to shard my database horizontally.

Why not use the facilities for sharding that are built in?

Database routers [1] are designed to allow you to have a single model
definition, but decide which database connection is used at runtime
based on properties of the objects involved.

[1] http://docs.djangoproject.com/en/dev/topics/db/multi-db/#automatic-database-routing

Yours,
Russ Magee %-)

Darius Damalakas

unread,
Jul 22, 2010, 9:53:09 AM7/22/10
to Django users
> What I'm trying to do is to shard my database horizontally.
Are you sure you are using the right tool for the right purpose?
Sharding IMHO should not be done by re-creating models for every shard

Andy

unread,
Jul 22, 2010, 6:59:08 PM7/22/10
to Django users
>
> Why not use the facilities for sharding that are built in?
>
> Database routers [1] are designed to allow you to have a single model
> definition, but decide which database connection is used at runtime
> based on properties of the objects involved.
>
> [1]http://docs.djangoproject.com/en/dev/topics/db/multi-db/#automatic-da...
>

I must have misunderstood the database routers then.

From the documentation:

"db_for_read(model, **hints)

Suggest the database that should be used for read operations for
objects of type model."

I thought that means that all objects of type model must be routed to
the same database.

Could you give me an example of a database router that would -
- first look at the type of the object, if it's of the type Tweet then
look at the userid of the Tweet's author, take a modulo N of the
userid, and then routes to DB0 if the modulo is 0, DB1 if the modulo
is 1, etc?

Thanks
Reply all
Reply to author
Forward
0 new messages