简体   繁体   English

如何为+ =:'DeferredAttribute'和'int'修复TypeError不支持的操作数类型

[英]How to fix TypeError unsupported operand type(s) for +=: 'DeferredAttribute' and 'int'

i am trying to create a fantasy-like app, i want the user's starting 11 to increase each time they add a player, and the team budget decrease by the amount of the player price. 我正在尝试创建一个类似幻想的应用程序,我希望用户每次添加玩家时都会增加11,并且团队预算会减少玩家价格。

i am using the django web framework for python. 我正在使用django web框架进行python。

here is my model for the user team 这是我的用户团队模型

class UserTeam(models.Model):
    name = models.CharField(max_length=50)
    user = models.OneToOneField(User, on_delete=models.CASCADE, default=None)
    players = models.ManyToManyField(Player)
    budget = models.IntegerField(default=100000000)
    FORMATION_CHOICES = (
        ('4-3-3', '4-DEF 3-MID 3-ATT'),
        ('4-4-2', '4-DEF 4-MID 2-ATT'),
        ('3-4-3', '3-DEF 4-MID 3-ATT'),
        ('5-3-2', '5-DEF 3-MID 2-ATT'),
    )
    formation = models.CharField(max_length=10, choices = FORMATION_CHOICES)
    starting_xi = models.IntegerField(default=0)
    bench_sub = models.IntegerField(default=0)
    total_score = models.IntegerField(default=0)
    round_score = models.IntegerField(default=0)
    Highest_team_score = models.IntegerField(default=0)


    @classmethod
    def transfer_player_in(cls, user, new_player):
        team, created = cls.objects.get_or_create(
            user=user
        )

        team.players.add(new_player)

    @classmethod
    def transfer_player_out(cls, user, new_player):
        team, created = cls.objects.get_or_create(
            user=user
        )

        team.players.remove(new_player)

my player model 我的球员模特

class Player(models.Model):
    POSITION_CHOICES = (
        ('ATT', 'Attacker'),
        ('MID', 'Midfielder'),
        ('DEF', 'Defender'),
        ('GK', 'GoalKeeper'),
    )
    name = models.CharField(max_length=50)
    FirstName = models.CharField(max_length=20)
    LastName = models.CharField(max_length=20)
    position = models.CharField(max_length=3, choices = POSITION_CHOICES)
    country = models.ManyToManyField(Country)
    photo = models.ImageField(blank=True)
    price = models.IntegerField(default=0)
    total_score = models.IntegerField(default=0)

here is the view function to handle transfer of players in and out 这里是用于处理进出球员转移的视图功能

def Transfer_Player(request, operation, pk):
    player= Player.objects.get(pk=pk)  #gets a particular player into the variable player
    if operation =='add':    #if user wants to buy player
        UserTeam.starting_xi += 1
        UserTeam.budget = UserTeam.budget - Player.price
        UserTeam.transfer_player_in(request.user, player)  #add player to team
        UserTeam.save

    elif operation =='sell':
        UserTeam.transfer_player_out(request.user, player)
    else:
        pass
    return redirect('/Fantasy')

i expect the user's starting 11 to increase by 1 each time they perform the add operation and the user budget to be decreased by the price of the player, but instead am getting a TypeError, unsupported operand type(s) for +=: 'DeferredAttribute' and 'int'. 我希望每次用户执行添加操作时用户的起始值11增加1,并且用户预算将减少播放器的价格,而是获得TypeError,+ =的不支持的操作数类型:'DeferredAttribute '和'int'。

You need to have a concrete instance of a UserTeam in order to do what you want. 您需要具有UserTeam的具体实例才能执行您想要的操作。 I suspect that your code should look like this: 我怀疑你的代码应该是这样的:


if operation =='add':    #if user wants to buy player
    team = UerTeam.objects.get(user_id=request.user.id)
    team.starting_xi += 1
    team.budget -= player.price
    team.save()
    UserTeam.transfer_player_in(request.user, player)  #add player to team

暂无
暂无

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

相关问题 类型错误:* 不支持的操作数类型:'DeferredAttribute' 和 'DeferredAttribute' django - TypeError: unsupported operand type(s) for *: 'DeferredAttribute' and 'DeferredAttribute' django 如何修复“TypeError:不支持的操作数类型-:‘NoneType’和‘int’” - How to fix 'TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'' 如何修复 Python TypeError:不支持的操作数类型“int”和“tuple” - How to fix Python TypeError: unsupported operand type(s) 'int' and 'tuple' 如何修复TypeError:+:'int'和'list'的不支持的操作数类型 - How to fix TypeError: unsupported operand type(s) for +: 'int' and 'list' 如何解决“ TypeError:+不支持的操作数类型:'int'和'NoneType'” - How to fix “TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'” 如何修复“ /:'int'和'function'的TypeError不支持的操作数类型”? - How to fix “TypeError unsupported operand type(s) for /: 'int' and 'function'”? 如何修复错误 - TypeError:不支持的操作数类型 - :'tuple'和'int' - How to fix error - TypeError: unsupported operand type(s) for -: 'tuple' and 'int' 如何解决,TypeError:%不支持的操作数类型:'NoneType'和'int' - How to fix, TypeError: unsupported operand type(s) for %: 'NoneType' and 'int' 如何修复TypeError:不支持的操作数类型? - How to fix TypeError: unsupported operand type(s)? 如何修复“ TypeError:不支持的操作数类型”? - How to fix “TypeError: unsupported operand type(s)”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM