Reputation: 171
I have a problem with my model and serialization, I do a inner join of two tables like this:
# 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)
the error shows me is = " 'CurrencySys' object is not iterable "
my file Wallet / Serialization is
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__'
my models are:
# 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)
why show me that error and how to fix this problem, please help me
thank for your attention.
Upvotes: 0
Views: 574
Reputation: 47364
currencysys
is a single ForeignKey object. So you dont need many=True
argument for it:
class WalletSerializer(serializers.ModelSerializer):
# currencysys = CurrencySysSerializer(many=True)
currencysys = serializers.StringRelatedField()
class Meta:
model = Wallet
fields = '__all__'
Upvotes: 1