Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading