简体   繁体   English

您如何在 django 中成功获取您想要的任何对象的 id?

[英]How do you successfully get the id of any of your objects that you want in django?

One piece of functionality I'm trying to build for a fantasy football app is to have a page where any logged in user can see a list of all the other teams and their scores so that you can compare your score to teams of other users.我正在尝试为梦幻足球应用程序构建的一项功能是拥有一个页面,任何登录的用户都可以在其中查看所有其他球队的列表及其分数,以便您可以将分数与其他用户的球队进行比较。 Earlier today I got some interesting advice that suggested making a function in views.py that can show any team - not just your own.今天早些时候,我得到了一些有趣的建议,建议在 views.py 中创建一个 function 可以显示任何团队 - 而不仅仅是您自己的团队。 The function would obviously need to get the id of the team you want to display. function 显然需要获取您要显示的团队的 ID。 It sounds logical, but I can't think of how to start/implement this though.这听起来合乎逻辑,但我想不出如何开始/实施它。

This is the model object in question.这是有问题的 model object。

class Team(models.Model):
    team_name = models.CharField(max_length=255, blank=True)
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True)
    quarter_back = models.ForeignKey(Quarterback, on_delete=models.CASCADE, null=True)
    running_back = models.ForeignKey(Runningback, on_delete=models.CASCADE, null=True)
    wide_receiver = models.ForeignKey(Widereceiver, on_delete=models.CASCADE, null=True)
    tight_end = models.ForeignKey(Tightend, on_delete=models.CASCADE, null=True)
    kicker = models.ForeignKey(Kicker, on_delete=models.CASCADE, null=True)

I've tried making a function but I get a 404 error because there's no id submitted to the URL in the first place.我尝试制作 function 但我收到 404 错误,因为首先没有提交给 URL 的 id。 How can you get the object's id if there is no id showing in the template's URL?如果模板的 URL 中没有显示 id,如何获取对象的 id? If I manually type the object's id integer at the end of the URL, it does get passed and the scoreboard function can access that object, but I need this to happen automatically. If I manually type the object's id integer at the end of the URL, it does get passed and the scoreboard function can access that object, but I need this to happen automatically. But how can you do this?但是你怎么能这样做呢?

def scoreboard(request, id):

    team = Team.objects.get(pk=id)

with a URL like this.像这样的 URL。

path('scoreboard/<int:id>', views.scoreboard, name="scoreboard")

Of course, it's easy to iterate through all the objects and their fields, render them out to a template, etc, but that's only half my battle.当然,很容易遍历所有对象及其字段,将它们渲染为模板等,但这只是我成功的一半。 I need to be able to have scoreboard() access all teams because I need to compare all the player's names to data stored in a variable that contains each player's individual score that is pulled from an API.我需要能够让 scoreboard() 访问所有球队,因为我需要将所有球员的姓名与存储在变量中的数据进行比较,该变量包含从 API 中提取的每个球员的个人得分。 If a player's name is in the variable that contains all the score information, I can then assign that score to a separate variable that can be later used to calculate a team's total score.如果一个球员的名字在包含所有得分信息的变量中,那么我可以将该得分分配给一个单独的变量,以后可以使用该变量来计算球队的总分。 But yeah, my stumbling block is how to successfully get the id of any and all teams in the first place.但是,是的,我的绊脚石是如何首先成功地获得任何和所有团队的 id。 Any advice is much appreciated.非常感谢任何建议。

first you need to get the object then you will get id of the object首先你需要得到 object 然后你会得到 object 的 id

Get the object of a model获取 model 的 object

   `params = {"fields_in_model": values}`
   `obj = Model.objects.get(**params)`

Get the Id from object从 object 获取 ID

   `id = obj.id`

You need to pass team object to the template(where html exists), then get id of the team object like team.id and pass this id to the href link.您需要将团队 object 传递给模板(其中存在 html),然后像 team.id 一样获取团队 object 的 id 并将此 id 传递给 href 链接。 Finally you can get id automatically from the path you mentioned above ('scoreboard/int:id', views.scoreboard, name="scoreboard")最后,您可以从上面提到的路径自动获取 id ('scoreboard/int:id', views.scoreboard, name="scoreboard")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM