如何在DJango中添加数据然后加入JOIN表

时间:2011-09-09 17:08:58

标签: python django django-models django-templates django-views

我有一个玩家列表,我想收集3个不同类别的统计数据。 (冲,过,接)。

每周玩家都会获得每个类别的新统计数据。

我希望能够做的是列出每个类别排名前5位的玩家,这些玩家的特定统计数据最多。我不太确定如何在我的views.py文件中设置查询,或者我是否需要在django中的模板文件中执行任何操作。

以下是我的模型的样子:

class Player(models.Model):

    first_name = models.CharField(
        max_length = 100,
        verbose_name = "First Name"
        )
    last_name = models.CharField(
        max_length = 100,
        verbose_name = "Last Name"
        )
    position = models.CharField(
        max_length = 2,
        choices = (('QB', 'QB',), ('RB', 'RB',), ('WR', 'WR',), ),
        blank = True
        )
    team = models.CharField(
        max_length = 30,
        choices = (('Dallas', 'Dallas',), ('New York', 'New York',), ('Tampa Bay', 'Tampa Bay',), ),
        blank = True
        )
    number = models.CharField(
        max_length = 3,
        blank = True
        )

class PlayerStat(models.Model):
    player = models.ForeignKey(Player)

    week_num = models.CharField(
        max_length = 10,
        choices = (('1', 'Sep 2nd',), ('2', 'Sep 9th',), ('3', 'Sep 16th',),('4', 'Sep 23rd',), ('5', 'Sep 30th',), ('6', 'Nov 2nd',),('7', 'Nov 7th',), ('8', 'Nov 14th',), ('9', 'Nov 21st',),('10', 'Nov 28th',), ('11', 'Dec 4th',), ('12', 'Dec 11th',), ),
        blank = True,
        null=True
        )
    rushing_attempts = models.CharField(
        max_length = 100,
        verbose_name = "Rushing Attempts",
        blank=True
        )
    rushing_yards = models.CharField(
        max_length = 100,
        verbose_name = "Rushing Yards",
        blank=True
        )
    rushing_touchdowns = models.CharField(
        max_length = 100,
        verbose_name = "Rushing Touchdowns",
        blank=True
        )
    passing_attempts = models.CharField(
        max_length = 100,
        verbose_name = "Passing Attempts",
        blank=True
        )
    passing_completions = models.CharField(
        max_length = 100,
        verbose_name = "Passing Completions",
        blank=True
        )
    passing_yards = models.CharField(
        max_length = 100,
        verbose_name = "Passing Yards",
        blank=True
        )
    passing_touchdowns = models.CharField(
        max_length = 100,
        verbose_name = "Passing Touchdowns",
        blank=True
        )
    receptions = models.CharField(
        max_length = 100,
        verbose_name = "Receptions",
        blank=True
        )
    receiving_yards = models.CharField(
        max_length = 100,
        verbose_name = "Receiving Yards",
        blank=True
        )
    receiving_touchdowns = models.CharField(
        max_length = 100,
        verbose_name = "Receiving Touchdowns",
        blank=True
        ) 

我是python和Django的菜鸟,所以任何帮助都会非常感激。

谢谢

1 个答案:

答案 0 :(得分:1)

首先,我必须更改模型,然后使用DecimalField作为统计数据 查询前五名的每个统计数据:

PlayerStat.objects.order_by('receiving_yards')[:5]

您还可以从

订购列出多个要订购的字段的订单
PlayerStat.objects.order_by('receiving_yards,receptions,receiving_touchdowns')[:5]