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

发布于 2024-12-03 23:12:42 字数 2774 浏览 0 评论 0原文

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

谢谢

I have a list of players that I would like to gather stats in 3 separate categories. (Rushing, Passing and Receiving).

Every week a player gets new stats for each category.

What I'd like to be able to do is list the top 5 players for each category ordered by who ever has the most yards for that particular statistic. I'm not quite sure how to set up the query(s) in my views.py file or if I need to do anything within my template file in django.

Here's what my models look like:

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
        ) 

I'm a noob to python and Django so any help would be greatly appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄离 2024-12-10 23:12:42

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

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

您还可以给出一个订单,列出多个要从中订购的字段

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

First of all I you have to change the model to use the DecimalField for the stats and then
query for each stat the top 5:

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

You can also give an order listing more than one field to order from

PlayerStat.objects.order_by('receiving_yards,receptions,receiving_touchdowns')[:5]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文