NENAD CIKIC
unread,Feb 5, 2012, 4:47:34 PM2/5/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to django...@googlegroups.com
Thanks. So to summarize the solution to filter inlines in admin interface based on userprofile was:
define the form on Y model as:
class YForm(forms.ModelForm):
request=None #NOTE THIS
lang=forms.ModelChoiceField(queryset=lang.objects.all()) #first get all
def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, initial=None, error_class=ErrorList,
label_suffix=':', empty_permitted=False, instance=None):
super(YForm, self).__init__(data, files, auto_id, prefix, initial, error_class, label_suffix,
empty_permitted, instance)
self.fields["lang"].queryset = lang.objects.filter(company__exact=self.request.user.get_profile().company)
class Meta:
model = Jelo_strani_lang
Where Lang model and userprofile has the same field "company" ; i.e. the user is part of that company, and the languages are defined at company level
Then I have defined the inline as:
class YInline(admin.StackedInline):
model = Y
form=YForm
ordering = ('lang',)
def get_formset(self, request, obj=None, **kwargs):
self.form.request=request #NOTE THIS
return super(YInline, self).get_formset(request, obj, **kwargs)
Hope that helps to someone