So I'm building a battleships game using Django. What I want to do is, once a game is finished, create a new Game object in my DB with the score and the current user. My Game model looks like this:
class Game(models.Model):
user = models.ForeignKey(User)
score = models.IntegerField(default=0)
date = models.DateTimeField(auto_now_add=True)
win = models.BooleanField(default=False)
def save(self, *args, **kwargs):
if self.score < 0:
self.score = 0
super(Game, self).save(*args, **kwargs)
def __unicode__(self):
return unicode(self.date)
The actual game is written in JS and you and the AI keep taking turns until one of you wins, at which point your score is calculated. This is where I would like to update my DB with the new game, but I have no idea how to go about it. I was thinking of putting in a submit button and making the view create a new game object on a POST request, but I don't know how I can pass the score & win/loss variables to it. Any help would be appreciated.