order - Django TypeError unsupported operand type(s) for +: 'dict' and 'int' -
i created through model can add order field m2m field having problems auto incrementing order field via def number() below. when add object, typeerror unsupported operand type(s) +: 'dict' , 'int' , i'm not sure why. 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']
as mentioned in docs, aggregate()
returns dict object. hence, variable last_order
dict , trying add number dict.
you should value dict , add.
return last_order['order__max'] + 1
Comments
Post a Comment