Opened 21 months ago

Last modified 21 months ago

#34444 closed Bug

Summing through a One-to-Many relationship on the property of a JSONField fails — at Initial Version

Reported by: Clément Escolano Owned by: nobody
Component: Database layer (models, ORM) Version: 4.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hello,

When summing through a One-to-Many relationship on the property of a JSONField, there is the following error

TypeError: the JSON object must be str, bytes or bytearray, not int

To reproduce the error, you can create a model that is linked to another model with a JSONField:

class Group(models.Model):
    name = models.TextField()

class Membership(models.Model):
    group = models.ForeignKey(Group, related_name="memberships")
    extra = models.JSONField()

Then create a group and a membership with an amount in the JSONField:

group = Group.objects.create(name="Test")
Membership.objects.create(group=group, extra={"amount": 5})

Finally, a request is made to sum the amount property for every group:

Group.objects.annotate(total=Sum("memberships__extra__amount"))

Note that the SQL query made by Django is correct but the code still raises the error

TypeError: the JSON object must be str, bytes or bytearray, not int

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top