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:
model = Rec
request.headers.get('x-requested-with') == 'XMLHttpRequest'
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
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.
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
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.
model = Rec
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.
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.)
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:
instance.url = reverse('name_of_url_1', args=[instance.pk])
instance.url = reverse('name_of_url_2', args=[instance.pk])
… 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.
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.