Home > Software engineering >  'tuple' object has no attribute '_meta'
'tuple' object has no attribute '_meta'

Time:12-19

why is my queryset throwing this error whenever i make a search to the backend of account object below the post have added the traceback ?

def auto_search(request):
    user = request.user
    search_query = request.GET.get('q')
    payload = []
    if search_query :
        search_results = Account.objects.filter(email__icontains=search_query).filter(username__icontains=search_query).distinct()
        user = request.user
        if user.is_authenticated:
            # get the authenticated users friend list
            auth_user_friend_list = FriendList.objects.get(user=user)
            for account in search_results:
                payload.append((account, auth_user_friend_list.is_mutual_friend(account)))
                
        payload = serializers.serialize('json',payload)       
        return JsonResponse({'status':200 , 'data':payload})

Account model.py

class Account(AbstractBaseUser):
    email = models.EmailField(unique=True)
    username = models.CharField(max_length=150, unique=True)
    first_name = models.CharField(max_length=150,blank=True)
    profile_image = models.ImageField(max_length=255,upload_to=get_profile_image_filepath, null=True, blank=True, default=get_default_profile_image)
    last_name = models.CharField(max_length=150,blank=True)
    phone_number = models.CharField(max_length=50, unique=True)
    date_of_birth = models.DateField(blank=True, null=True)

friendlist models.py

class FriendList(models.Model):
    user                = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="user")
    friends             = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True, related_name="friends") 
    
    # set up the reverse relation to GenericForeignKey
    
    notifications       = GenericRelation(Notification)

This is the traceback below

Traceback (most recent call last):
  File "C:\Users\Umar\Desktop\dreams\env\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Umar\Desktop\dreams\env\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Umar\Desktop\dreams\game\views.py", line 35, in auto_search
    payload = serializers.serialize('json',payload)
  File "C:\Users\Umar\Desktop\dreams\env\lib\site-packages\django\core\serializers\__init__.py", line 129, in serialize
    s.serialize(queryset, **options)
  File "C:\Users\Umar\Desktop\dreams\env\lib\site-packages\django\core\serializers\base.py", line 94, in serialize
    concrete_model = obj._meta.concrete_model
AttributeError: 'tuple' object has no attribute '_meta'

CodePudding user response:

I assume you're using django.core.serializers. The second argument you should pass to serializers.serialize() has to be queryset, usually it's a list of models django queryset.

Here you are collecting your list for serialization:

payload.append((account, auth_user_friend_list.is_mutual_friend(account)))

So each element of your list is tuple, which couldn't be serialized. Don't sure what you were exactly are going to do. But if you need list of accounts and FriendList has suitable method is_mutual_friend, you may wanted something like this:

            for account in search_results:
                if auth_user_friend_list.is_mutual_friend(account):
                    payload.append(account)

That should work correctly.

  • Related