Accessing ManyToMany Field in models.py (helper function)

14 views
Skip to first unread message

Michael Muster

unread,
Nov 8, 2013, 2:07:00 PM11/8/13
to django...@googlegroups.com
Hi there,

I want to collect some statistics about a card game.

So i have three models in my app:

Player = holds the player data
Hand = the data for one round
Partie = The data for a complete game

class Player(models.Model):
name = models.CharField(max_length=200, unique=True)
color = models.CharField(max_length=200, unique=True)
def __unicode__(self):
return '%s' % self.name
class Meta:
ordering = ['-name',]

class Hand(models.Model):
player = models.ForeignKey(Player)
date_created = models.DateTimeField('Date',default=datetime.datetime.now)
date_changed = models.DateTimeField('Date',default=datetime.datetime.now)
round = models.IntegerField("Runde", blank=True, null=True)
points_round = models.IntegerField("Points this round", blank=True, null=True)
phase = models.IntegerField("Phase",blank=True,null=True)
points_partie = models.IntegerField("All Points",blank=True, null=True)
def __unicode__(self):
return "%s - %s - %s" % (self.player, self.rount, self.date_created)
class Meta:
ordering = ['player', '-round']

class Partie(models.Model):
player = models.ManyToManyField(Player)
hand = models.ManyToManyField(Hand)
date_created = models.DateTimeField('Datum',default=datetime.datetime.now)
def __unicode__(self):
return '%s' % self.date_created
class Meta:
ordering = ['date_created']
def get_phase(self):
phase = Partie.objects.filter(spieler=self.spieler).count()
return phase


In "get_phase" in the Partie model i want to use "round" and points_round" to
calculate in which phase the player is.

In short: I want to be able to do:
phase_raw = Partie.objects.filter(player=self.player, partie=self.id, round=self.round, points_round__lte=9).count()

I know that this does not work, it would work for the two mentioned fields if i put the function into the Hand-Model, but
then i miss the "partie.id" field.

Any hints are welcome...

Best regards,
michael





Reply all
Reply to author
Forward
0 new messages