如何在django中定义两个玩家模型?

时间:2017-04-08 05:14:12

标签: python django django-models

我无法为django中的两个玩家定义模型。我以某种方式设法定义了这样的模型。

models.py

from django.db import models
from datetime import datetime

class Player(models.Model):
    name=models.CharField(max_length=40)
    score = models.IntegerField(default = 0) 

    def __unicode__(self):
        return self.name

    def __str__(self):
        return self.name

class Match(models.Model):
    player_one = models.ForeignKey(Player, on_delete=models.CASCADE, related_name='player_one')
    player_two = models.ForeignKey(Player, on_delete=models.CASCADE, related_name='player_two')

class Score_log(models.Model):
    match = models.ForeignKey(Match, on_delete=models.CASCADE)
    log_time=models.DateTimeField(auto_now_add=True, blank=True)

    class Meta:
        ordering = ["-match","-log_time"]

在匹配模型中,player_oneplayer_two已定义,因为我想验证具有django表单的那些。 此外,我无法使用Match.player_one.all()检索我的对象,并说'ReverseManyToOneDescriptor' object has no attribute 'all'。帮助_me_out?

0 个答案:

没有答案