1
2
3
4
5
6
    def formfield_for_manytomany(self, db_field, request, **kwargs):
        if db_field.__class__.__name__ == "ManyToManyField" and \
            db_field.rel.to.__name__ == self.model.__name__:
            kwargs['queryset'] = db_field.rel.to.objects.prefetch_related("user")
        return super(UserProfileInline, self).formfield_for_manytomany(
            db_field, request, **kwargs)