getting error 'tuple' object has no attribute 'value'

Moto
class budgetSerializer(serializers.ModelSerializer):
       
    budget_used        = serializers.SerializerMethodField()
    budget_remain      = serializers.SerializerMethodField()
    total_budget       = serializers.IntegerField(default=250000)


    def get_budget_used (self,obj):
            budget_used=budget.objects.filter(user=obj.user).aggregate(Sum('budget_stord__bytes'))
            data = []
            for val in budget_used.items():
                if val.value > total_budget :
                    raise serializers.ValidationError('you exceed the total budget limit')
           
            return f'budget : {(budget_used/1000)} '

    def get_total_budget(self,obj):
            total_budget = 250000
            return f'total_budget: {(total_budget/1000)} '   
    def get_remain(self,obj):
            remain = (total_budget) - (budget_used)   
            return remain

    class Meta:
        model  = budget_u
        fields = ("budget_used","total_budget","budget_remain")
MODEl 

class budget_u(CreatedModel, UUIDPKModel):
    budget_stord = models.ForeignKey('bud.Store', on_delete=models.SET_NULL, null=True, blank=True)
    user = models.ForeignKey('user.user', on_delete=models.SET_NULL, null=False, blank=False)

I need output like

{
    " budget_used": "100",
    "remain": "150G",
    "total_budget": "250"
} 

getting AttributeError: 'tuple' object has no attribute 'value'

Willem Van Onsem

You do not access the value with .value, .items() returns an iterable of 2-tuples, so you can use iterable unpacking in the for loop. But here you can simply use subscripting to access the value:

def get_budget_used(self, obj):
    budget_used = budget_u.objects.filter(
        user=obj.user
    ).aggregate(total=Sum('budget_stord__bytes'))['total'] or 0
    if budget_used > 250000:
        return 'you exceed the total budget limit'
    return f'budget : {(budget_used/1000)}'

That being said, it is rather odd to each time return strings. Often serializers return numeric data, since that is easier to process, for example by a JavaScript script at the browser side, or an integration of the client.

Este artigo é coletado da Internet.

Se houver alguma infração, entre em [email protected] Delete.

editar em
0

deixe-me dizer algumas palavras

0comentários
loginDepois de participar da revisão

Artigos relacionados

Getting a object has attribute error

AttributeError: 'tuple' object has no attribute 'write' error while writing into a file

Error "'tuple' object has no attribute 'replace'" but it's a list

Getting AttributeError error 'str' object has no attribute 'get'

Getting error AttributeError: ResultSet object has no attribute 'find_all'

restframework 'tuple' object has no attribute '_meta'

Tensorflow: AttributeError: 'tuple' object has no attribute 'eval'

Python - AttributeError: 'tuple' object has no attribute 'fire'

'tuple' object has no attribute 'ordered' on UpdateView

Getting 'Attribute Error - Str has no attribute GET'

Upgrade Plone 3.3.6 to either Plone 4.3.7 or Plone 5 (Error: 'tuple' object has no attribute 'remove')

Attribute Error: 'QueryDict' object has no attribute 'iterlists'

Attribute Error:'NoneType' object has no attribute 'parent'

Attribute Error at/ 'str' object has no attribute 'makefile'

Getting AttributeError: 'DataFrame' object has no attribute 'shape'

Getting " 'Request' object has no attribute 'Name'" in Flask

AttributeError: 'tuple' object has no attribute 'write' , instance segmentation python

AttributeError: 'tuple' object has no attribute 'log_softmax'

Python Pandas to_datetime AttributeError: 'tuple' object has no attribute 'lower'

'ExtraTreesClassifier' object has no attribute 'estimators_' Error

Error: 'float' object has no attribute 'isna'"

Django error - 'function' object has no attribute 'MyForm'

'NoneType' object has no attribute 'fillna' Error

Ansible The error was: 'unicode object' has no attribute

getting error while using Flask JWT, AttributeError: 'list' object has no attribute 'id' and shows 500 Internal server error

attribute error: list object has not attribute lstrip in sending an email with attachment

Python Attribute Error: 'NoneType' object has no attribute 'find_all'

Attribute error: 'list' object has no attribute 'strftime' while using pygooglenews

Getting : error while evaluating conditional (my_result.stat.exists): 'bool object' has no attribute 'stat' while expcting to be dictionary