Hello,
Is it possible to create a class in views.py that filters the records based on the user accessing it?
I don’t want it to filter according to admin or not (which I already have) but to filter according to the zone (field of the users table) that the user belongs to. For its part, the record also has a zone field. The thing would be to show zone 1 records to zone 1 users, zone 2 to zone 2 users…
I currently have the following generic view:
class ListadoReclamacion(ListView):
model = Rec
def get_queryset(self):
return self.model.objects.filter(estado_cod_id=1)
def get(self,request,*args,**kwargs):
request.headers.get('x-requested-with') == 'XMLHttpRequest'
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
return HttpResponse(serialize('json',self.get_queryset()),'application/json')
else:
return redirect('inicio_reclamaciones')
Side note: In addition to the official docs for class-based views, I also recommend the Classy Class-Based Views site and the CBV diagrams page for enhancing your understanding of how CBVs work. I strongly encourage you to become familiar with both of these resources.
Thanks for the information, I have been reading it and today I will read it again to understand more. One thing I wanted to ask, I already managed to filter the view according to some filters indicated in a queryset, but I would like to go further and apart from the filters according to a series of fields I would also like to filter the records depending on the user.
Example,
Firstly I filter the view according to field =1 but I want to add that if the user is =1 it shows it, if the user is =2 it doesn’t.
Sorry, I didn’t mean that. The thing is different.
I already have the records filtered according to the field and according to the user it shows it or not but what I don’t know how to do is the following:
If I access with user=1 and it shows the record, there must be a button that goes to a view
If I access with user=2 and it shows the record, there must be a button that goes to a different view than that of user 1
Well that would be the idea, according to the user, modify the url of the button, which in the end would be the view. Because I have two different views that would be one for each user.
url button 1 does one thing
url button 2 does something else
Yes, there is another field that I want to use for this, it is a field of 0 or 1.
Currently there is only one view created for the two types of user, what I do before is make a queryset that filters data for me.
View where I see what user it is, if it is 3 I make a query but another. But of course, the record view makes it perfect for me but the button is the same, which I don’t want.
class ListadoReclamacion(ListView):
model = Rec
def get_queryset(self):
if self.request.user.cod_cargo_id==3:
return self.model.objects.filter(estado_cod=4,del_dep_code=self.request.user.cod_delegacion_zona.cod_delegacion)
else:
return self.model.objects.filter(estado_cod=1,del_dep_code=self.request.user.cod_delegacion_zona.cod_delegacion)
The thing is that unlike this code that goes to an html page which is where it shows the records, buttons… In my case, as I said before, I bring the records from DataTable, I don’t know how it should be indicated in the part of the render
Yes, I understand what you’re current view (ListadoReclamacion) is doing.
What I am understanding what you want to do, is to change the JSON response being returned by that view to include one of two urls to be rendered in your page by your JavaScript, to change the value currently being rendered by this line:
If my understanding is correct, then I come back to my earlier question.
What is your Python code to figure out which of the two URLs to use?
(I’d be expecting to see an if statement here. And again, I’m asking you to ignore all other issues at the moment. I only want to see the code that will choose between the two URLs.)
Great! Now, instead of writing “send url” and “send another url”, what are the actual URLs that you want to use? (What are “url” and “another url” going to be?)
Now that you have the queryset object in this function, you can iterate over the queryset to add additional attributes to the individual elements. Copying your code from your previous responses:
.... continued from above
for instance in queryset:
if self.request.user.cod_cargo_id==3:
instance.url = reverse('name_of_url_1', args=[instance.pk])
else:
instance.url = reverse('name_of_url_2', args=[instance.pk])
return instance
… where name_of_url_1 is the name attribute for the url defined as reclamaciones/enviar_para_aprobar/<int:pk>/ and name_of_url_2 is the name attribute of the other url.
First, your return instance statement is indented too far. It needs to be at the “main” level of your code in that function. (At the same level of indentation as the for statement, not indented within it.)
No, you haven’t fundamentally changed get_queryset. As a function, it’s returning a queryset.
Side notes:
This line is useless and can be removed.
This would be more appropriate if you changed it to return a JsonResponse instead of an HttpResponse.