models.py
class User(models.Model):
googleId = models.CharField(max_length=512, primary_key=True, default='')
imageURL = models.CharField(max_length=512, null=True)
userName = models.CharField(max_length=512, null=True)
firstName = models.CharField(max_length=512, null=True)
lastName = models.CharField(max_length=512, null=True)
#phoneNumberRegex = RegexValidator(regex=r"^ ?1?\d{8,15}$")
phoneNumber = models.CharField(max_length=512, null=True)
email1 = models.CharField(max_length=512, blank=False)
email2 = models.CharField(max_length=512, blank=True)
bio = models.TextField(blank=True)
planId = models.ForeignKey('primal_user.Plans',
on_delete=models.CASCADE,
default="Free")
password = models.CharField(max_length=512, null=True)
accountCreationDate = models.DateTimeField(auto_now_add=True)
coins = models.IntegerField(default=2)
assetsDownloaded = models.IntegerField(default=0)
assetsPurchased = models.IntegerField(default=0)
class Asset(models.Model):
assetId = models.CharField(max_length=20, primary_key=True)
devUserId = models.ForeignKey(User, on_delete=models.CASCADE)
keywordId = models.ForeignKey(Tags, on_delete=models.CASCADE)
assetName = models.CharField(max_length=50, null=False)
description = models.TextField(blank=True)
features = models.TextField(blank=True)
uploadedDate = models.DateField(auto_now_add=True)
typeId = models.BooleanField(default=True)
paidStatus = models.BooleanField(default=False)
price = models.IntegerField(null=True)
size = models.FloatField(null=False)
downloadCount = models.BigIntegerField(null=True)
version = models.CharField(max_length=10)
serializer.py
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = '__all__'
class AssetSerializer(serializers.ModelSerializer):
class Meta:
model = Asset
fields = '__all__'
views.py
class UserAsset(APIView):
def get(self,request,devUserId):
try:
user = Asset.objects.filter(devUserId=devUserId).values()
serializer = AssetSerializer(user, many= True)
return Response(serializer.data)
except Asset.DoesNotExist:
raise Http404
I am a beginner in Django, so am unable to figure out what the issue is. I tried looking for solutions to similar questions but could not resolve the issue. I was getting attribute error, then it was resolved after I entered many=True in AssetSerializer but now I am stuck with this KeyError. A while trying to figure out the error, I noticed that this error is thrown while executing serializer.data. Thank You for any help possible.
CodePudding user response:
In your code, just a small change is needed in syntax.
Instead of:
user = Asset.objects.filter(devUserId=devUserId).values()
Write:
user = Asset.objects.filter(devUserId=devUserId)
And that should solve the issue!
CodePudding user response:
try with
user = Asset.objects.values('fieldnamehere').filter(devUserId=devUserId)