gpt4 book ai didi

python - Django 获取 "first_name"是此函数的无效关键字参数“创建模型类的实例时出现类型错误

转载 作者:太空宇宙 更新时间:2023-11-03 14:21:34 24 4
gpt4 key购买 nike

使用 Django 1.11 和 django-pyodbc-azure 最新版本(如果相关)。

我是 Django 新手,一直遵循 1.11 教程,在此之前没有任何问题,但我感到非常困惑。

这是我的 models.py:

from django.db import models

# Create your models here.


class Player(models.Model):
first_name = models.CharField(max_length=20, name='First Name')
last_name = models.CharField(max_length=20, name='Last Name')

def __str__(self):
return '{}, {} ({})'.format(self.last_name, self.first_name, self.id)


class Game(models.Model):
players = models.ManyToManyField(Player, name='Players')

def __str__(self):
return ' vs. '.join(self.players)


class Round(models.Model):
GIN = 'GI'
UNDERCUT = 'UN'
KNOCK = 'KN'
ENDING_ACITONS = (
(GIN, 'Gin'),
(UNDERCUT, 'Undercut'),
(KNOCK, 'Knock'),
)

game = models.ForeignKey(Game, on_delete=models.CASCADE, name='Parent Game')
winner = models.ForeignKey(Player, on_delete=models.CASCADE, name='Winning Player')
points = models.IntegerField(name='Points Awarded')
end = models.CharField(max_length=2, choices=ENDING_ACITONS)

def __str__(self):
return '{} awarded {} points via {}'.format(self.winner, self.points, self.end.name)

现在当我运行manage.py shell并输入:

from game.models import *
bobby = Player(first_name='Bobby', last_name='Fisher')

我遇到了这个错误:

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Users\mteper\PycharmProjects\GinRummy\venv\lib\site-packages\django\db\models\base.py", line 571, in __init__
raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
TypeError: 'first_name' is an invalid keyword argument for this function

任何见解都将不胜感激,因为正如我之前所说,我对 Django 和 Python 总体来说是新手。

最佳答案

name 参数导致了问题。您应该设置 verbose_name相反。

class Player(models.Model):
first_name = models.CharField(max_length=20, verbose_name='First Name')
last_name = models.CharField(max_length=20, verbose_name='Last Name')

关于python - Django 获取 "first_name"是此函数的无效关键字参数“创建模型类的实例时出现类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47912530/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com