Sunday, February 5, 2012

Re: Filter admin inlines

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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/jXQbP5KZOCEJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment