Thursday, February 28, 2013

Problem with 'ModelForm' vs 'Permission'

Dear All.

I've something like this :
--------START---------
class L2GroupForm(ModelForm):
    #Try to lock ForeignKey field to single value
    class Meta:
        model = L2Group
        widgets = {
            'company_id': TextInput(),
        }


class L2GroupAdmin(admin.ModelAdmin):
    #Choose the Form
    form = L2GroupForm()
    #Filtering Listed groups, only to which have right company_id
    def queryset(self, request):
        requserid =request.session.get('_auth_user_id', None)
        qs = self.model._default_manager.get_query_set()

        if not requserid is None :
            company_filter = L2User.objects.get(pk=requserid).company_id
            if not company_filter is None:
                qs = qs.filter(company_id=company_filter)
        print qs
        return qs

    #Try to set 'company_id' field initial value
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == 'company_id':
            requserid =request.session.get('_auth_user_id', None)
            if not requserid is None :
                company_filter = L2User.objects.get(pk=requserid).company_id
                if not company_filter is None:
                    kwargs['initial'] = company_filter
            return db_field.formfield(**kwargs)
        return super(L2GroupAdmin, self).formfield_for_foreignkey(
            db_field, request, **kwargs
        )

l2admin.register(L2Group, L2GroupAdmin)

--------STOP----------

Problem is :
1. When I loged in (as superuser), it says that I've no right to edit anything, but
2. When I commented the ' form = L2GroupForm()' , all is works but the company_id field not locked to single choice ... yes the initial value setup is work.

Kindly please give me your enlighten to lock a ForeignKey field to single choice.

Sincerely
-bino-

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment