2👍
Ok, I hate to throw a monkey wrench in your works, but you seriously don’t need to use threadlocals hacks.
The user is in the request object which means there is no need to extract it using middleware. It’s passed as an argument to every view.
The trick to limiting your form choices is therefore to dynamically change the queryset used in the form as opposed to doing a limit choices in the model.
So your form looks like this:
# forms.py
from django import forms
from project.app.models import Activity
class ActivityForm(forms.ModelForm):
class Meta:
model Activity
and your view will look like this:
# views.py
...
form = ActivityForm(instance=foo)
form.fields['job_operators'].queryset = \
User.objects.filter(operators__set=bar)
...
This is just a quick sketch, but should give you the general idea.
If you’re wondering how to avoid threadlocals in the admin, please read Users and the admin by James Bennett.
Edit: Useful form tricks in Django by Collin Grady also shows an example of dynamically setting the queryset in the form __init__ method, which is cleaner than my example above.
2👍
But what about the fields you define with list_filter? They only respect limit_choices_to. So if you want to limit the choices of Django Admin filters you have to use limit_choices_to and some Middleware to filter them based on the current user. Or is there an easier solution?
- [Django]-Celery what happen to running tasks when using app.control.purge()?
- [Django]-Heroku server error (500) when Debug = False , whitenoise could not find style.css
- [Django]-A better django datetime filter at the admin panel
0👍
Monkey Wrenches welcome friend!
I’d also found another way, though it seems less direct:
class ActivityForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(ActivityForm, self).__init__(*args, **kwargs)
if self.initial['job_record']:
jr = JobRecord.objects.get(pk=self.initial['job_record'])
self.fields['operators'].queryset = jr.operators
class Meta:
model = Activity
exclude = ('duration',)
I think your ways is better! Thanks heaps!