【问题标题】:How do I add data then JOIN tables in DJango如何在 DJango 中添加数据然后加入表
【发布时间】:2011-09-09 17:08:57
【问题描述】:

我有一份球员名单,我想收集 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 的菜鸟,所以任何帮助都将不胜感激。

谢谢

【问题讨论】:

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


    【解决方案1】:

    首先,我必须更改模型以使用 DecimalField 进行统计,然后 查询每个统计的前 5 名:

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

    您还可以为一个订单列出多个字段以供订购

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

    【讨论】:

      猜你喜欢
      • 2023-02-25
      • 1970-01-01
      • 2012-10-05
      • 1970-01-01
      • 2020-08-22
      • 1970-01-01
      • 2021-04-21
      • 2019-09-13
      • 1970-01-01
      相关资源
      最近更新 更多