Django TypeError unsupported operand type(s) for +: 'dict' and 'int'

bayman

I created a through model so I can add an order field to the m2m field but I am having problems auto incrementing the order field via def number() below. When I add an object, I get TypeError unsupported operand type(s) for +: 'dict' and 'int' and i'm not sure why. Any ideas?

models.py:

class Playlist(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
    name = models.CharField(max_length=50)
    tracks = models.ManyToManyField(Track, through='PlaylistTrack')

    def __str__(self):
        return self.name

class PlaylistTrack(models.Model):

    def number():
        last_order = PlaylistTrack.objects.all().aggregate(Max('order'))
        if last_order == None:
            return 1
        else:
            return last_order + 1

    track = models.ForeignKey(Track)
    playlist = models.ForeignKey(Playlist)
    order = models.PositiveIntegerField(default=number)

    class Meta:
        ordering = ['order']
JRodDynamite

As mentioned in the docs, aggregate() returns a dict object. Hence, the variable last_order is actually a dict and you are trying to add a number to a dict.

You should get the value from the dict and then add.

return last_order['order__max'] + 1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Django unsupported operand types

From Dev

unsupported operand types for //: 'str', 'int'

From Dev

Unsupported operand type(s) for +: 'dict' and 'int'

From Dev

TypeError: unsupported operand type(s) for +=: 'int' and 'list'

From Dev

TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

From Dev

TypeError: unsupported operand int and NoneType? Python

From Dev

TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

From Dev

TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'

From Dev

TypeError: unsupported operand type(s) for |: 'int' and 'str'

From Dev

TypeError: unsupported operand type(s) for +: 'int' and 'Element'

From Dev

TypeError: unsupported operand type(s) for //: 'int' and 'graph'

From Dev

TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'

From Dev

TypeError For Unsupported Operand type 'int' and 'list'

From Dev

TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

From Dev

TypeError: unsupported operand type(s) for 'Instance' and 'Int'

From Dev

TypeError: unsupported operand type(s) for +=: 'int' and 'str'

From Dev

TypeError: unsupported operand type(s) for <<: 'int' and 'float'

From Dev

TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'

From Dev

TypeError: unsupported operand type(s) for +: 'int' and 'instance'

From Dev

typeError: unsupported operand type(s) for %: 'list' and 'int'

From Dev

Django- unsupported operand type(s) for /: 'dict' and 'dict'

From Dev

TypeError: unsupported operand type(s) for +: 'dict_keys' and 'list'

From Dev

Unsupported operand types 'NoneType'

From Dev

TypeError: unsupported operand type(s) for -: 'str' and 'int' Writing a story?

From Dev

Sum function prob TypeError: unsupported operand type(s) for +: 'int' and 'str'

From Dev

Python TypeError: unsupported operand type(s) for ^: 'float' and 'int'

From Dev

TypeError: unsupported operand type(s) for -: 'str' and 'int' Python

From Dev

Keras reports TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

From Dev

Code throws TypeError: unsupported operand type(s) for -: 'int' and 'list'

Related Related

  1. 1

    Django unsupported operand types

  2. 2

    unsupported operand types for //: 'str', 'int'

  3. 3

    Unsupported operand type(s) for +: 'dict' and 'int'

  4. 4

    TypeError: unsupported operand type(s) for +=: 'int' and 'list'

  5. 5

    TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

  6. 6

    TypeError: unsupported operand int and NoneType? Python

  7. 7

    TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

  8. 8

    TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'

  9. 9

    TypeError: unsupported operand type(s) for |: 'int' and 'str'

  10. 10

    TypeError: unsupported operand type(s) for +: 'int' and 'Element'

  11. 11

    TypeError: unsupported operand type(s) for //: 'int' and 'graph'

  12. 12

    TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'

  13. 13

    TypeError For Unsupported Operand type 'int' and 'list'

  14. 14

    TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

  15. 15

    TypeError: unsupported operand type(s) for 'Instance' and 'Int'

  16. 16

    TypeError: unsupported operand type(s) for +=: 'int' and 'str'

  17. 17

    TypeError: unsupported operand type(s) for <<: 'int' and 'float'

  18. 18

    TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'

  19. 19

    TypeError: unsupported operand type(s) for +: 'int' and 'instance'

  20. 20

    typeError: unsupported operand type(s) for %: 'list' and 'int'

  21. 21

    Django- unsupported operand type(s) for /: 'dict' and 'dict'

  22. 22

    TypeError: unsupported operand type(s) for +: 'dict_keys' and 'list'

  23. 23

    Unsupported operand types 'NoneType'

  24. 24

    TypeError: unsupported operand type(s) for -: 'str' and 'int' Writing a story?

  25. 25

    Sum function prob TypeError: unsupported operand type(s) for +: 'int' and 'str'

  26. 26

    Python TypeError: unsupported operand type(s) for ^: 'float' and 'int'

  27. 27

    TypeError: unsupported operand type(s) for -: 'str' and 'int' Python

  28. 28

    Keras reports TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

  29. 29

    Code throws TypeError: unsupported operand type(s) for -: 'int' and 'list'

HotTag

Archive