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:
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With