Django BaseManager.all() got an unexpected keyword argument 'Login'

Question:

I have database:

class Users(models.Model):
     Login = models.OneToOneField(User, on_delete=models.CASCADE) <--- one to one field
     email = models.EmailField(max_length=255, unique=True, verbose_name='mail')
     balance = models.IntegerField(blank=False, verbose_name='balance')
     bonus = models.IntegerField(blank=True, verbose_name='count_bonus')

     def __str__(self):
         return str(self.Login.username)

     class Meta:
         verbose_name = 'Users'
         verbose_name_plural = 'Users'
         ordering = ['id']

and i need to query the database to get all the values by request.user, but i get the error.

BaseManager.all() got an unexpected keyword argument ‘Login’

def Account(request):

    user = Users.objects.all(Login=request.user)

    context = {
        'title' : 'title',
        'user' : user,
    }
    return render(request=request, template_name='site/account.html', context=context)
Asked By: TASK

||

Answers:

You are trying to apply query on the all() method which Django doesn’t allow, it is simply used to return all model objects.

What you need is to apply filter() on the query set. Please note that applying filter() on model object applies to all objects by default and you don’t need to explicitly use all().

def Account(request):

  user = Users.objects.filter(Login=request.user)

  context = {
      'title' : 'title',
      'user' : user,
  }
  return render(request=request, template_name='site/account.html', context=context)
Answered By: umar

You should use Queryset.filter method instead. Queryset.all just gives you the queryset to retrieve all the records.

...
user = Users.objects.filter(Login=request.user)
...

However, your Users model has a one-to-one relation to the User, so you can define related_name, for example

class Users(models.Model):
     Login = models.OneToOneField(User, related_name="user_info", on_delete=models.CASCADE)
     ...

Then, you will be able to access related Users object this way

...
user = request.user.user_info
...
Answered By: Dmytro
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.