“ Currencysys”对象不可迭代

时间:2018-07-31 14:07:35

标签: django api django-rest-framework

我的模型和序列化有问题,我对两个表进行了内部连接,如下所示:

# file views.py from Wallet
class WalletBalances(generics.ListAPIView):
      serializer_class = WalletSerializer

    def get_object(self, current_user):

        return Wallet.objects.filter(currencysys__id=F('currencysys_id'), 
                                 user_id=current_user)


    def get(self, request, format=None):
        current_user = request.user

        list_balances = self.get_object(current_user.id)
        serializer = self.serializer_class(list_balances, many=True)
        get_data = serializer.data

        return JsonResponse({'data': get_data}, safe=False, 
                              status=status.HTTP_200_OK)

错误显示我是=“'CurrencySys'对象不可迭代”

我的文件钱包/序列化为

from other_project.serializers import CurrencySysSerializer

class WalletSerializer(serializers.ModelSerializer):
      # currencysys = CurrencySysSerializer(many=True)
      currencysys = serializers.StringRelatedField(many=True)

     class Meta:
         model = Wallet
         fields = '__all__'

我的模型是:

# Currency / models.py
class CurrencySys(models.Model):
      currency_symbol = models.CharField(max_length=45, blank=True)
      currency_name = models.CharField(max_length=45, blank=True)
      currency_status = models.BooleanField(blank=True)
      currency_crypto = models.BooleanField(blank=True)

# wallet / models.py
class Wallet(models.Model):
      addresskey = models.CharField(max_length=40, blank=True)
      name_wallet = models.CharField(max_length=45, blank=True)
      currencysys = models.ForeignKey(CurrencySys, 
                                  on_delete=models.CASCADE)
      user = models.ForeignKey(User, on_delete=models.CASCADE)

为什么向我显示该错误以及如何解决此问题,请帮助我

感谢您的关注。

1 个答案:

答案 0 :(得分:1)

null是单个ForeignKey对象。因此,您不需要 Strike%/Total Visited = DIVIDE ( SUM ( Table1[Strike] ), CALCULATE ( SUM ( Table1[Visited] ), FILTER ( Table1 Table1[year] = "2018" ) ) ) 参数:

currencysys