BCA
BCA

Reputation: 438

Django Query result comparison with if statement

Need to check browser changeable values with using browser inspect and enter random id into url which is in button so I'm not really pro about django and stuff.I make a validation of data which is going to updated or deleted that can checked by tenantid for instance Im going press update on a single line of record and mechanics going to check if the record tenantid is equal to my tenant id or not. problem is about I cant compare query result to int value.

if any better approach about this I'ill be glad to hear, thank you have a good days.

@login_required()  def customer_update(request, pk):
    customer = get_object_or_404(Customer, pk=pk)
    valueOwnerTenantid = Customer.objects.values_list('tenantid',   flat=True).filter(id=pk)
    print(valueOwnerTenantid)
    print(request.user.tenantid)
    print(valueOwnerTenantid.query)

    if number(valueOwnerTenantid) == number(request.user.tenantid):
        if request.method == 'POST':
            print('Equal and POST')
            form = CustomerForm(request.POST, instance=customer)
        else:
            print('Equal and Get')
            form = CustomerForm(instance=customer)
            return save_customer_form(request, form, 'customer_update_partial.html')
    else:

Upvotes: 2

Views: 225

Answers (3)

Z.Amir
Z.Amir

Reputation: 195

You can try something like this if you are looking for records with specific tenant_id

my_tenant_id = request.user.tenantid
Customer.objects.filter(tenantid=my_tenant_id)

or what Willem Van Onsem suggested for single record

Upvotes: 3

Mikey Lockwood
Mikey Lockwood

Reputation: 1261

Customer.objects.values_list('tenantid', flat=True).filter(id=pk) will return a list instead of a single value. It also looks like you're getting the same Customer object twice

You can remove the valueOwnerTenantid = Customer.objects.values_list('tenantid', flat=True).filter(id=pk) line and just use customer.tennantid to get the tennantid

if customer.tennantid == request.user.tenantid:
   ...

Upvotes: 2

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477794

You are only interested in a single value, not a QuerySet of values, so you should use .get(..) instead of .filter() here. But in fact you can even omit it completely, since you already retrieved the tenantid in the customer.

Your view thus should look like:

@login_required()
def customer_update(request, pk):
    customer = get_object_or_404(Customer, pk=pk)
    if customer.tenantid == request.user.tenantid:
        # ...

that being said, it is rather weird that your store an id in your models, and not a ForeignKey to some Tenant model, etc. Making references without ForeignKeys is typically not a good idea: it is possible that tenantids do not refer to real tenants, for example. Furthermore by default Django will not add an index to the tenantid column, making searching for the customer with a given tenantid more computationally expensive.

Upvotes: 4

Related Questions