Django query filter by another dataset

I have 2 User models:

  1. User: Default user model: enter image description here

  2. Account: Custom User model, which is an extension of the defualt user model, as I wanted to add a custom field to it called 'Status'.

enter image description here

The problem is that I wanted to filter the data based on the current User, so id usually do something like:

Account.objects.filter(usernmae = User).values_list('status', flat=True)

The problem is that the Account dataset doesnt have the username but they both have the same ID.

I was thinking of doing something like this:

Status = Account.objects.filter(user_id=User.objects.filter(username = user).values_list('id', flat=True)).values_list('status', flat=True)

But then i get the following error:

enter image description here

I imagine there is a way better way of doing it, if yall could help me out.

def upload_audits(request):
    form = audits_form(request.POST or None, request.FILES or None)
    if form.is_valid():
        form = audits_form()
        obj = Csv_Audit.objects.get(activated=True)
        with open(obj.file_name.path,'r') as f:
            #to clear model contents before applying new data

            reader = csv.reader(f)

            for i,row in enumerate(reader):
                if i==0:
                    user = row[1] # gets data from CSV Table and returns username
                    Status = Account.objects.filter(user_id = request.user).values_list('status')

                        qs_login = user,
                        Status = Status,



            obj.activated = True

    return render(request,"backend/uploads.html",{'form':form})

class Account(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    status = models.CharField(('Status'), max_length=200, default='',choices = [('Bau','Bau'),('Week 1','Week 1')])

    def __str__(self):
        return self.user.username

1 Answer:mahdi rahimi

I tried your method with the following code:

Status = Account.objects.filter(user__username = user).values_list('status', flat=True)

Which resulted in the following error:

enter image description here

And then I thought of doing this:

Status = Account.objects.filter(user = user).values_list('status', flat=True)

But i got this error:

enter image description here

Which actually returns the usernmae but it seems to be asking for an int?

1 answer

  • answered 2021-09-12 08:56 mahdi rahimi

    based on my experience, you can simply join the two tables, and get what you want. roughly, it translates to this:

    result = Account.objects.filter(user__username = user).values_list('status', flat=True)

    or you can do two queries if you are comfortable with that.

    found_user_id = User.objects.filter(username = user).values_list('id', flat = True)
    result = Account.objects.filter(user_id = found_user_id).values_list('status', flat=True)

    hope that helped.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum