0

I get this error when I try to do a query set on the Django model

'AppUser' object is not subscriptable

despite it is working normally in a print statement but the error only appears when I put it in an IF statement

here is my code :

    def to_representation(self, instance):
        data = super().to_representation(instance)
        print("reached here") #print normaly
        print(AppUser.objects.filter(mobile=instance['mobile']).exists()) #print normally (False)
        if AppUser.objects.filter(mobile=instance['mobile']).exists(): # Raises an Exception
            if instance.playerprofile_set.all().count() > 0:
                player_profile = instance.playerprofile_set.all()[0]
                data['player_profile'] = PlayerProfileSerializer(
                  player_profile).data
                for item in Team.objects.all():
                    if player_profile in item.players.all():
                        data['team'] = TeamSerializer(item).data
                    if item.cap.id == player_profile.id:
                        data['team'] = TeamSerializer(item).data
                    # data["team"] = instance.
        return data

UPDATE

And here is my AppUser class:

class AppUser(models.Model):


    first_name = models.CharField(max_length=33)
    last_name = models.CharField(max_length=33)
    mobile = models.CharField(max_length=33)
    email = models.EmailField(null=True, blank=True, max_length=33)
    birthdate = models.DateField(null=True, blank=True)
    password = models.CharField(max_length=33)
    confirm_password = models.CharField(max_length=33)
    image = models.FileField(upload_to="uploads", null=True, blank=True)
    main_user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    generated_code = models.PositiveIntegerField(null=True, blank=True)
    user_langauge = models.CharField(max_length=33, default="en")
    dark_mode = models.BooleanField(default=False)

    def __str__(self):
        return str(self.mobile) + " " + str(self.first_name) + " " + str(self.last_name)

so calling AppUser.objects.filter() should return a queryset or empty query set, and when adding exists() should return a True or

2 Answers 2

2

Instead of count, use exists():

if AppUser.objects.filter(mobile=instance['mobile']).exists():
   if instance.playerprofile_set.exists():
            player_profile = instance.playerprofile_set.first()

Because it is very efficient in contrast to count() and runs a very small query.

To your original problem, it is not possible to guess what is wrong from the sample code, specially when print works, if not.

Update

I think the error should be with this code:

instance['mobile']

Here instance should be an object, so instance.mobile or data['mobile'] should work.

Sign up to request clarification or add additional context in comments.

3 Comments

Changed to exists() ,, same error still and I have added some more info in the question
Can you add the full error stacktrace please?
Yes it was the error causing , i used it to make sure that object is not created , but it caused the problem
0

try this:

 def to_representation(self, instance):
            data = super().to_representation(instance)
            print("reached here") #print normaly
            print(AppUser.objects.filter(mobile=instance['mobile']).count() > 0) #print normally (False)
            if AppUser:
              AppUser.objects.filter(mobile=instance['mobile']).count() > 0
              if instance.playerprofile_set.all().count() > 0:
                    player_profile = instance.playerprofile_set.all()[0]
                    data['player_profile'] = PlayerProfileSerializer(
                      player_profile).data
                    for item in Team.objects.all():
                        if player_profile in item.players.all():
                            data['team'] = TeamSerializer(item).data
                        if item.cap.id == player_profile.id:
                            data['team'] = TeamSerializer(item).data
                        # data["team"] = instance.
            return data

1 Comment

Badly it gave the same result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.