views:

33

answers:

1

I'm trying to initialize the form attribute for MyModelAdmin class inside an instance method, as follows:

class MyModelAdmin(admin.ModelAdmin): 
    def queryset(self, request):
        MyModelAdmin.form = MyModelForm(request.user)

My goal is to customize the editing form of MyModelForm based on the current session. When I try this however, I keep getting an error (shown below). Is this the proper place to pass session data to ModelForm? If so, then what may be causing this error?

TypeError at ...

Exception Type: TypeError

Exception Value: issubclass() arg 1 must be a class

Exception Location: /usr/lib/pymodules/python2.6/django/forms/models.py in new, line 185

A: 

Hi, i use queryset fot filtering records, maybe this example help you:

.....
.....
def queryset(self, request):
    cuser = User.objects.get(username=request.user)

    qs = self.model._default_manager.get_query_set()
    ordering = self.ordering or () # otherwise we might try to *None, which is bad ;)

    if ordering:
        qs = qs.order_by(*ordering)

    qs = qs.filter(creator=cuser.id)

    return qs
Saff