繁体   English   中英

Django的表单添加返回“ NoneType”对象的项目是不可调用的错误

[英]django form to add item returning 'NoneType' object is not callable error

我的add_player视图采用team_season_id参数并显示未绑定的表单。

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

这是PlayerForm:

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带任何模型的ModelForm 根据文档创建具有model属性的内部Meta类:

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

另外,如果您确实不需要save ,请不要覆盖它。

暂无
暂无

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

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