Is there a way to create a new database table other than model?

4 views
Skip to first unread message

vbs

unread,
Nov 23, 2010, 5:14:58 AM11/23/10
to Django users
Hi all,

This is my situation, I want to make a web based game-hall.
I have a model named Game. When I add a new game from the admin panel,
a new record will be added to the database table assigned to model
Game. This is what django does.
And at the same time, I also want a new database table to be created,
named as the name of the new game, or with some prefix. It's best to
have a model assigned to this table.

Is there any way to do this?

Thank you.

ringemup

unread,
Nov 23, 2010, 8:26:15 AM11/23/10
to Django users
Perhaps there's another way to accomplish your goal. Why do you want
a table for each game?

S Basl

unread,
Nov 23, 2010, 8:42:44 AM11/23/10
to django...@googlegroups.com
If you absolutely need to do so, you can execute raw sql queries to do whatever you need using Django. The docs are here: http://docs.djangoproject.com/en/1.2/topics/db/sql/

However, it's probably a 'bad' idea to go mucking about with sql if you can at all avoid it.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Li You

unread,
Nov 23, 2010, 8:54:22 AM11/23/10
to django...@googlegroups.com
Is there a prettier way to do it?

Maybe a dynamic model, or nested model, or something like?

--
Best Regards!

Li You
University of Science and Technology of China

Li You

unread,
Nov 23, 2010, 8:55:14 AM11/23/10
to django...@googlegroups.com
Because I want to save user's info about each game. And the best way
is to save the info by each game.

S Basl

unread,
Nov 23, 2010, 9:10:59 AM11/23/10
to django...@googlegroups.com
None that I know of, but I am no Django guru. I get the feeling I'm missing something about your situation however. It seems like you should be able to accomplish what you want, saving unique info about each game, without per-game tables. You have a many to many relationship between games and users (ie each user can have multiple games & each game can have multiple users), yes?  If so, you can store additional information in the bridge table that Django creates. Check the docs for ManyToMany fields: http://docs.djangoproject.com/en/1.2/ref/models/fields/#django.db.models.ManyToManyField & http://docs.djangoproject.com/en/1.2/topics/db/models/#intermediary-manytomany

bruno desthuilliers

unread,
Nov 23, 2010, 9:17:44 AM11/23/10
to Django users
On 23 nov, 14:55, Li You <vbs0...@gmail.com> wrote:
> Because I want to save user's info about each game. And the best way
> is to save the info by each game.

This doesn't mean having a distinct table per game - unless of course
the database table schema is specific for each game but then you want
something a bit more elaborate than raw sql since you'll have to write
per-game specific code.

Else - I mean, if you have the same table schema for each game
"infos", you just need a foreign key on the game - no table creation
involved at all.

David De La Harpe Golden

unread,
Nov 23, 2010, 9:23:22 AM11/23/10
to django...@googlegroups.com
On 23/11/10 13:55, Li You wrote:
> Because I want to save user's info about each game. And the best way
> is to save the info by each game.
>

It sounds like you have some db modelling confusion here.

Are you sure you don't just want a many-to-many between Game and User
with some data on an explicit through model "UserGameInfo" with some
per-user per-game data?

http://docs.djangoproject.com/en/1.2/topics/db/models/#extra-fields-on-many-to-many-relationships

Now, maybe you mean that each game instance is a different /kind of
game/, and the per-game per-user data would be wildly different
for each game instance, but then you probably would use different
models for the different kinds of game.


Li You

unread,
Nov 23, 2010, 10:13:57 AM11/23/10
to django...@googlegroups.com
>> Because I want to save user's info about each game. And the best way
>> is to save the info by each game.
>>
>
> It sounds like you have some db modelling confusion here.
>
> Are you sure you don't just want a many-to-many between Game and User
> with some data on an explicit through model "UserGameInfo" with some
> per-user per-game data?
>
> http://docs.djangoproject.com/en/1.2/topics/db/models/#extra-fields-on-many-to-many-relationships

ok, this is a way.

Let me describe this problem in more detail:
I have a Game model for each game. This model can be seen as a list of
all games, such as,

class Game(models.Model):
name = models.CharField(max_length = 128, unique = True)
available = models.BooleanField(default = True)
timestamp = models.DateTimeField(auto_now_add=True)

And usually a user just plays with a few of games, but there maybe
hundreds of games.
A user may have different level in different games, so I need to keep
record of the user's level in each game.

At the beginning, I want to create a table for each game. Only users
who play that game get their level logged in the game's table.

By using many-to-many field, I can record the level info of all games
in a single table. But I am a little afraid of the performance...

That is the origin of this question. Thank you all. :)

--
Best Regards!

Tom Evans

unread,
Nov 23, 2010, 10:26:43 AM11/23/10
to django...@googlegroups.com
On Tue, Nov 23, 2010 at 3:13 PM, Li You <vbs...@gmail.com> wrote:
> Let me describe this problem in more detail:
> I have a Game model for each game. This model can be seen as a list of
> all games, such as,
>
> class Game(models.Model):
>    name = models.CharField(max_length = 128, unique = True)
>    available = models.BooleanField(default = True)
>    timestamp = models.DateTimeField(auto_now_add=True)
>
> And usually a user just plays with a few of games, but there maybe
> hundreds of games.
> A user may have different level in different games, so I need to keep
> record of the user's level in each game.
>
> At the beginning, I want to create a table for each game. Only users
> who play that game get their level logged in the game's table.
>
> By using many-to-many field, I can record the level info of all games
> in a single table. But I am a little afraid of the performance...
>
> That is the origin of this question. Thank you all. :)
>

"We should forget about small efficiencies, say about 97% of the time:
premature optimization is the root of all evil. Yet we should not pass
up our opportunities in that critical 3%.A good programmer will not be
lulled into complacency by such reasoning, he will be wise to look
carefully at the critical code; but only after that code has been
identified" - Knuth

Your design actually described a through table for M2M, but you don't
want to do that because you fear that the performance will be bad.
Implement your design, and then work out whether performance will be
bad.

Even if it is, relational databases are designed to be used in this
manner. You could (depending upon the capabilities of your database)
partition the through table based upon the game type. With this
database configuration, you would have the effect of having one table
per game type, but without adding unnecessary complexity to your
software.

Cheers

Tom

Reply all
Reply to author
Forward
0 new messages