gpt4 book ai didi

python - django 表单添加项目返回 'NoneType' 对象不可调用错误

转载 作者:行者123 更新时间:2023-11-28 23:05:12 24 4
gpt4 key购买 nike

我的 add_player View 采用 team_season_id 参数并显示未绑定(bind)的表单。

def add_player( request, team_season_id ):
team_season = get_object_or_404( TeamSeasonConference, pk=team_season_id )

player_form = PlayerForm()
return render_to_response( 'backoffice/organization/add_player.html', {
'player_form': player_form,
'team_season': team_season,
'player': None
}, context_instance=RequestContext( request ) )

这是回溯:

Traceback:
File "/usr/lib/pymodules/python2.6/django/core/handlers/base.py" in get_response
92. response = callback(request, *callback_args, **callback_kwargs)
File "/mypath/apps/account/decorators.py" in __call__
73. return self.view_func(request, *args, **kwargs)
File "/mypath/apps/backoffice/views.py" in add_player
877. player_form = PlayerForm()
File "/usr/lib/pymodules/python2.6/django/forms/models.py" in __init__
218. self.instance = opts.model()

Exception Type: TypeError at /backoffice/roster/add/player/2/
Exception Value: 'NoneType' object is not callable

这是玩家表单:

class PlayerForm( forms.ModelForm ):
CLASS_CHOICES = (
('', '-----'),
('FR', 'freshman'),
('RS-FR', 'red-shirt freshman'),
('SO', 'sophomore'),
('RS-SO', 'red-shirt sophomore'),
('JR', 'junior'),
('RS-JR', 'red-shirt junior'),
('SR', 'senior'),
('RS-SR', 'red-shirt senior'),
)

first_name = forms.CharField( max_length=100, required=False, label="First" )
last_name = forms.CharField( max_length=100, label="Last" )

jersey = forms.IntegerField( min_value=0, required=False, label="Jersey" )
height = forms.CharField( max_length=10, required=False )
weight = forms.IntegerField( required=False, min_value=0 )
age = forms.IntegerField( required=False, min_value=0 )
year = forms.TypedChoiceField( choices=CLASS_CHOICES, required=False, empty_value='' )
varsity_letters = forms.IntegerField( required=False, min_value=0 )
position = forms.CharField( max_length=20, required=False )
hometown = forms.CharField( required=False )
major = forms.CharField( max_length=100, required=False )
freeform_school = forms.CharField( required=False, label="HS" )
description = forms.CharField (widget=forms.Textarea, required=False )
HSSN = forms.CharField( max_length=8, required=False )
mugshot = forms.FileField( widget=forms.FileInput(), required=False )
team_season_connection = forms.ModelMultipleChoiceField(queryset=TeamSeasonConference.objects.filter(team = 1), required=False, widget=forms.SelectMultiple)

def save(self, commit=True, request=None, user=None):
player = super( PlayerForm, self ).save( commit=False )

if commit:
# save the player
player.save()
return player

这是播放器模型

class Player( models.Model ):

CLASS_CHOICES = (
('FR', 'freshman'),
('RS-FR', 'red-shirt freshman'),
('SO', 'sophomore'),
('RS-SO', 'red-shirt sophomore'),
('JR', 'junior'),
('RS-JR', 'red-shirt junior'),
('SR', 'senior'),
('RS-SR', 'red-shirt senior'),
)

first_name = models.CharField( max_length=100 )
last_name = models.CharField( max_length=100 )
suffix = models.CharField( max_length=5, blank=True )
nickname = models.CharField( max_length=100, blank=True )
slug = models.SlugField( max_length=200, blank=True, help_text="Used for associating content. (lowercase, hyphens sted spaces, no punctuation)" )

team_season_connection = models.ManyToManyField( TeamSeasonConference, related_name='career_roster', blank=True)
team_season = models.ForeignKey( TeamSeasonConference, related_name='roster', blank=True )
height = models.CharField( max_length=10, blank=True )
weight = models.PositiveIntegerField( blank=True, null=True )
age = models.PositiveIntegerField( blank=True, null=True )
year = models.CharField( max_length=5, choices=CLASS_CHOICES, blank=True, db_index=True )
varsity_letters = models.PositiveIntegerField( blank=True, null=True )
position = models.CharField( max_length=20, blank=True )
jersey = models.PositiveIntegerField( blank=True, null=True )
mugshot = models.ForeignKey( 'content.Photo', null=True, blank=True )
profile = models.ForeignKey( 'content.Story', null=True, blank=True, help_text="Optionally link player to a profile story.")

major = models.CharField( max_length=100, blank=True)

freeform_school = models.CharField( max_length=100, blank=True, help_text="Take care to enter the school consistantly across players." )
hometown = models.CharField( max_length=100, blank=True, help_text="Town and state, i.e. Orlando, FL." )
offered_scholarship = models.BooleanField( help_text="Check if user has a scholarship offer." )
description = models.TextField( blank=True )

antagonist = models.BooleanField( help_text="To be used as indicator of whether this player is the season's antagonist" )
pluck_user = models.ForeignKey ( 'auth.User', null=True, blank=True, help_text="Optionally link to user login")

# reverse generic relations
scrapbooks = generic.GenericRelation( Scrapbook )

最佳答案

您使用的 ModelForm 没有关联模型。创建一个带有 model 属性的内部 Meta 类,如 per documentation :

class PlayerForm(forms.ModelForm):
class Meta:
model = models.Player # or whatever

此外,如果您真的不需要它,请不要覆盖 save

关于python - django 表单添加项目返回 'NoneType' 对象不可调用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6477931/

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