'When i try to query django model by positive small integer field i get error
When i run the following code i get the error "TypeError: Field 'app' expected a number but got <AppChoices.SUK: 1>"
class Category(models.Model):
class AppChoices(models.Choices):
ASK_EMBLA = 0
SUK = 1
---
class SellerReview(models.Model):
------
app = models.PositiveSmallIntegerField(
choices=Category.AppChoices.choices,
default=Category.AppChoices.ASK_EMBLA)
rating = models.PositiveSmallIntegerField()
-----
class RentPostDetailSearializer(serializers.ModelSerializer):
---
def get_posted_by_brief(self, obj: RentPost):
-----
rating = 0
ratings = list(poster_profile.seller_reviews.filter(
app=Category.AppChoices.SUK).values_list("rating", flat=True)) #---> issue here
if ratings:
rating = sum(ratings)/len(ratings)
---
Don't know why is this happening even though the App choices are integers and supposed to be numbers
Solution 1:[1]
If the values are integers, you should use models.IntegerChoices instead of models.Choices.
Unless using the IntegerChoices base class specifically, Enum members are not integers.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | solarissmoke |
