当我尝试通过正小整数字段查询 Django 模型时出现错误
When i try to query django model by positive small integer field i get error
当我 运行 以下代码时,我收到错误“TypeError: Field 'app' expected a number but got ”
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)
---
不知道为什么会发生这种情况,即使应用选择是整数并且应该是数字
如果值是整数,您应该使用 models.IntegerChoices
而不是 models.Choices
。
除非特别使用 IntegerChoices
基础 class,Enum
成员 are not integers。
当我 运行 以下代码时,我收到错误“TypeError: Field 'app' expected a number but got
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)
---
不知道为什么会发生这种情况,即使应用选择是整数并且应该是数字
如果值是整数,您应该使用 models.IntegerChoices
而不是 models.Choices
。
除非特别使用 IntegerChoices
基础 class,Enum
成员 are not integers。