finally get this issue fixed
@@ -17,45 +17,32 @@ class IconBoxInline(TabularDynamicInlineAdmin):
class HomePageAdmin(PageAdmin):
inlines = (IconBoxInline,)
+
class UserQuotaForm(forms.ModelForm):
user = forms.ModelChoiceField(queryset=User.objects.all())
class Meta:
model = UserQuota
fields = ['user', 'allocated_value', 'used_value', 'unit', 'zone']
- exclude = []
-
- def __init__(self, *args, **kwargs):
- super(UserQuotaForm, self).__init__(*args, **kwargs)
- if self.instance and self.instance.user_id:
- # change a record, user foreign key cannot be changed
- self.fields['user'].initial = self.instance.user
- self.fields['user'].widget.attrs['readonly'] = True
- self.fields['user'].widget.attrs['disabled'] = True
- # def clean_user(self):
- # if self.instance.pk is None:
- # return self.instance.user
- # else:
- # return self.cleaned_data['user']
- #
def save(self, *args, **kwargs):
instance = super(UserQuotaForm, self).save(commit=False)
- self.cleaned_data['user'].save()
+ instance.user = self.cleaned_data['user']
return instance
+
class QuotaAdmin(admin.ModelAdmin):
model = UserQuota
- form = UserQuotaForm
- # list_select_related = True
- # list_editable = ('user',)
- #readonly_fields = ('user',)
- #add_fieldsets = (
- # (None, {
- # 'classes': ('wide',),
- # 'fields': ('user',)}),
- #)
+
list_display = ('user', 'allocated_value', 'used_value', 'unit', 'zone')
- list_filter = ('zone',)
+ list_filter = ('zone', 'user__username', )
+
+ def get_form(self, request, obj=None, **kwargs):
+ # use a customized form class when adding a UserQuota object so that
+ # the foreign key user field is available for selection.
+ if obj is None:
+ return UserQuotaForm
+ else:
+ return super(QuotaAdmin, self).get_form(request, obj, **kwargs)
admin.site.register(HomePage, HomePageAdmin)
