additional filed in the many to many model

19 views
Skip to first unread message

汤宝

unread,
Feb 25, 2013, 1:44:02 AM2/25/13
to python...@googlegroups.com
Hi there
  i'm trying to add some fileds to the many to many model, like blow:

class Skill(StdModel):
  skill_id=SymbolField()

class Player(StdModel):
  name=SymbolField()
  skills = ManyToManyField(Player, related_name='players')

now, i wan to add a "skill_level" filed to the Player_Skill many2many model, can i?

thanks!


Message has been deleted

lsbardel

unread,
Feb 25, 2013, 2:57:13 AM2/25/13
to python...@googlegroups.com

  i'm trying to add some fileds to the many to many model, like blow:

class Skill(StdModel):
  skill_id=SymbolField()


You have two options. the simplest:

class Skill(StdModel):
    code = SymbolField()
    level = IntegerField()
 
class Player(StdModel):
  name=SymbolField()
  skills = ManyToManyField(Player, related_name='players')

now, i wan to add a "skill_level" filed to the Player_Skill many2many model, can i?
  
class Player(StdModel):
    name = SymbolField()
    skills = ManyToManyField(Skill, related_name='players')

Alternatively, If you want a separate Skill model (without level in it) than you need to use a *through* model:

class Skill(StdModel):
    code = SymbolField(unique=True)

class SkillLevel(StdModel)
    level = IntegerField()

class Player(StdModel):
    name = SymbolField()
    skills = ManyToManyField(Skill, through=SkillLevel, related_name='players')
 

汤宝

unread,
Feb 26, 2013, 2:53:16 AM2/26/13
to python...@googlegroups.com
Thank you, lsbardel !
I'll try "though".
stdnet is a very nice framework!

在 2013年2月25日星期一UTC+8下午3时57分13秒,lsbardel写道:

汤宝

unread,
Feb 26, 2013, 3:44:51 AM2/26/13
to python...@googlegroups.com
Another question:

now i can add skillLevel as blow:

class Skill(StdModel):
    code = SymbolField(unique=True)

class SkillLevel(StdModel)
    level = IntegerField()

class Player(StdModel):
    name = SymbolField()
    skills = ManyToManyField(Skill, through=SkillLevel, related_name='players')


player1 = Player(name='tom').save()
skill1 = Skill(code='fireball').save()
player1.skills.add(skill1, level = 1).save()

BUT, how can i access player1's skill level of fireball from a query?

thanks!

在 2013年2月25日星期一UTC+8下午3时57分13秒,lsbardel写道:

汤宝

unread,
Feb 26, 2013, 4:38:59 AM2/26/13
to python...@googlegroups.com
Hi again.

i saw *throughquery* of class Many2ManyRelatedManager, so i can get a ManyToMany through model's by

for skill_level in Play.models.filter(name='tom')[0].skills.throughquery().items():
    skill_level.level

but there's only a throughquery, and NO throughfilter. so i think it's impossible to query a many-many models filed in StdNet's current version.

Thanks


在 2013年2月25日星期一UTC+8下午2时44分02秒,汤宝写道:

lsbardel

unread,
Feb 26, 2013, 5:38:14 AM2/26/13
to python...@googlegroups.com
i saw *throughquery* of class Many2ManyRelatedManager, so i can get a ManyToMany through model's by

for skill_level in Play.models.filter(name='tom')[0].skills.throughquery().items():
    skill_level.level


Correct, the *throughquery* method will give you a Query on the actual model which is linking the two models in the many-to-many relationship.
 
but there's only a throughquery, and NO throughfilter. so i think it's impossible to query a many-many models filed in StdNet's current version.

The *throughquery* method returns a Query and therefore you can chain additional query-methods such as filter, exclude and so forth

    Player.objects.get(name='tom').skills.throughquery().filter(...)

The ManyToManyField is not properly documented unfortunately

汤宝

unread,
Feb 26, 2013, 5:45:21 AM2/26/13
to python...@googlegroups.com
wow, chain additional query-methods! that's great!
i think i can solve the problem

thanks for your help again!

在 2013年2月26日星期二UTC+8下午6时38分14秒,lsbardel写道:
Reply all
Reply to author
Forward
0 new messages