python - Django model with relationships -
i know, questions of kind have been asked @ least bunch of times here, kind of starting django , having tough time apply solutions similar questions , put stuff together.
i think model should have 3 classess:
class guild(models.model): name = models.charfield(max_length=50) class battle(models.model): guild1 = models.foreignkey(guild, related_name="guild_one") guild2 = models.foreignkey(guild, related_name="guild_two") # tournament = models.foreignkey(tournament) ??? class tournament(models.model): name = models.charfield(max_length=50) ????
a battle
war between 2 guilds
, takes place during tournament
. there can many battles
in tournament
. guild
can participate in several tournaments
.
i want able search guild (to see every battle of theirs, every tournament have taken part in), tournament see battles have been played out plus have access through tournament see guilds have ever participated. above code (plus line commeneted out) work, because feel maybe tournament class should have reference battle?
also, if give example of query take guilds' names participated in given tournament.
the model battle
fine place having foreign key tournament, can remove comment, although should use quotation marks referencing tournament
, since it's declared after battle
, i.e. models.foreignkey('tournament')
.
to participating guilds' names in list format, use backwards relation objects guild_one
, guild_two
of guild
object, q objects , .values_list()
. this:
>>> django.db.models import q >>> guild.objects.filter(q(guild_one__tournament=t) |\ ... q(guild_two__tournament=t)).values_list('name', flat=true) [u'red', u'blue', u'green', u'black']
assuming t
references tournament
.
Comments
Post a Comment