0

The issue currently is filtering for all users that

  1. Are not the user
  2. Where model field == True

If I remove the ".filter(model="True")" than the first requirement is met. How can I add more filters to the users?

Current error message:

FieldError at /explore/ Cannot resolve keyword 'model' into field. Choices are: date_joined, email, emailaddress, favorite, first_name, groups, id, images, is_active, is_staff, is_superuser, last_login, last_name, logentry, owner, password, profile, socialaccount, user_permissions, username, webhook

I understand that the error means. However, I'm not sure how to implement it in the code.

view.py

def explore(request, pk=None):
    template_name = 've/cp/explore.html'

    users_list = User.objects.exclude(id=request.user.id).filter(model="True")

    paginator = Paginator(users_list, 16)  # Show x per page
    page = request.GET.get('page')
    users = paginator.get_page(page)

    try:
        favorite = Favorite.objects.get(current_user=request.user)
        favorites = favorite.users.all()
    except Favorite.DoesNotExist:
        favorites = None

    args = {
        'favorites': favorites, 'users': users,
    }

    return render(request, template_name, args)

models.py

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.TextField(max_length=500, blank=True, null=True)
    model = models.BooleanField(default=False)
    ...

1 Answer 1

1

Use __ with profile keyword:

users_list = User.objects.exclude(id=request.user.id).filter(profile__model="True")
Sign up to request clarification or add additional context in comments.

4 Comments

Are you my guardian angel? This worked perfectly.. I'm a bit disappointed I did not realize that. If I need to add more filters could I keep adding .filter()
@AlexWinkler you can just pass additional args to single filter: filter(profile__model="True", email="[email protected]"). This will filter records wich fit both conditions.
@AlexWinkler check this section of doc: docs.djangoproject.com/en/dev/topics/db/queries/…
@AlexWinkler If you wondering about profile.. in the answer it is related name that django automtically generates for the reverse relationship.(ie, User model to the Profile)

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.