django admin fieldsets - 添加相关模型的字段

时间:2014-07-14 11:12:06

标签: django django-forms django-admin django-models

我的模型EmployeeUser具有OneToOne关系。我试图在Employee管理员中添加一些用户字段(first_name, last_name, username, email),以便直接从员工添加/更改表单中编辑这些字段,但我仍然没有想出如何做到这一点。

我看到另一次以类似的方式做这种事情,但现在我有一个字段集错误:

Unknown field(s) (first_name) specified for Employee. Check fields/fieldsets/exclude attributes of class EmployeeAdmin

这里是代码:

# FIELDSETS
default_employee_fieldset = (
    ('General', {
        'fields': (
                ('user', 'full_name',),
            ),
        }),
    # ..OTHER FIELDS NOT INCLUDED
    )

finance_fields_employee_fieldset = [
    ('General', {
        'fields': (
                ('full_name',),
                ('first_name', 'last_name')
        ),
        }),
    # ..OTHER FIELDS NOT INCLUDED
    ]



# ADMIN FORM
class EmployeeAdminForm(forms.ModelForm):
    class Meta:
        model = Employee

    def __init__(self, *args, **kwargs):
        super(EmployeeAdminForm, self).__init__(*args, **kwargs)
        self.fields['first_name'] = forms.CharField(_('first name'), max_length=30, blank=True)
        self.fields['last_name'] = forms.CharField(_('last name'), max_length=30, blank=True)

        if 'instance' in kwargs:
            user = kwargs['instance'].user
            self.fields['first_name'].initial = user.first_name
            self.fields['last_name'].initial = user.last_name



# MODEL ADMIN
class EmployeeAdmin(ExtendedAdmin):
    list_display = ('user', 'department', 'line_manager', 'acting_line_manager', 'jobtitle', 'office', 'payroll_id',)
    search_fields = ('user__username', 'user__first_name', 'user__last_name',
                     'line_manager__user__first_name', 'line_manager__user__last_name')
    list_filter = ('department', 'grade', 'clearance', 'office', 'user__is_active', 'user__is_staff',
                   'ready_for_paid_work')
    fieldsets = default_employee_fieldset

    def changelist_view(self, request, extra_context=None):
        extra_context = {'title': 'Employee details'}
        return super(EmployeeAdmin, self).changelist_view(request, extra_context)

    def change_view(self, request, object_id, form_url='', extra_context=None):
        if object_id:
            try:
                record = Employee.objects.get(id=object_id)
                extra_context = {'title': 'Edit employee record: %s' % (str(record),)}
            except (ValueError, Employee.DoesNotExist):
                pass
        # Generate a new CSRF token as this page contains sensitive data
        rotate_token(request)
        return super(EmployeeAdmin, self).change_view(request, object_id, form_url, extra_context)

    def queryset(self, request):
        qs = super(EmployeeAdmin, self).queryset(request)
        if request.user.is_superuser:
            return qs
        elif request.user.has_perm('myapp.change_all_employees'):
            if request.user.has_perm('myapp.change_ex_employees'):
                return qs.filter(user__is_staff=True)
            else:
                return qs.filter(user__is_staff=True, user__is_active=True)
        else:
            return qs.filter(user__is_staff=True, user__is_active=True).filter(
                Q(line_manager=request.user.employee) |
                Q(acting_line_manager=request.user.employee) |
                Q(user=request.user)
            )

    def get_form(self, request, obj=None, *args, **kwargs):
        self.form = EmployeeAdminForm
        if obj:
            if request.user.has_perm('myapp.change_hr_employee_data'):
                self.readonly_fields = ('user', )
                self.exclude = None
                self.fieldsets = finance_fields_employee_fieldset
            elif request.user.has_perm('myapp.change_employee_data_operations'):
                if request.user == obj.user:
                    # set readonly fields
                    self.exclude = None
                    self.fieldsets = finance_fields_employee_fieldset
                else:
                     # set readonly fields
                    self.exclude = ('employer_pension_contribution', 'employee_pension_contribution',)
                    self.fieldsets = default_employee_fieldset
            else:
                # if user is in AAA department and not employee's linemanager and not own record
                if request.user.employee.department == settings.DPTS['AAA']
                    if request.user == obj.user:
                        # set readonly fields
                    else:
                        # set readonly fields
                        self.exclude = ('account_number', 'sort_code', 'salary',
                                        'employer_pension_contribution', 'employee_pension_contribution',)
                        self.fieldsets = default_employee_fieldset
                else:
                    # if user is editing his own record
                    if request.user == obj.user:
                        # set readonly fields
                        self.exclude = ('account_reference', 'payroll_id',)
                        self.fieldsets = finance_fields_employee_fieldset[0:-2]
                    # if user is editing some for whom is line manager
                    else:
                        if request.user.employee == obj.line_manager or \
                                        request.user.employee == obj.acting_line_manager:
                            # set readonly fields
                            self.fieldsets = default_employee_fieldset
                            self.exclude = ('employer_pension_contribution', 'employee_pension_contribution',)
        else:
            self.readonly_fields = ()
            self.exclude = ()
            self.fieldsets = default_employee_fieldset
        form = super(EmployeeAdmin, self).get_form(request, *args, **kwargs)
        form.request = request        
        return form



    def save_model(self, request, obj, form, change):
        if change:
            old_obj = Employee.objects.get(id=obj.id)
            if obj.jobtitle != old_obj.jobtitle:
                employee_detail_change_notification(obj, 'job title', obj.jobtitle.name)
            if obj.line_manager != old_obj.line_manager:
                employee_detail_change_notification(obj, 'line manager', obj.line_manager)
            if obj.acting_line_manager != old_obj.acting_line_manager:
                employee_detail_change_notification(obj, 'acting line manager', obj.acting_line_manager)
            if obj.home_address_line1 != old_obj.home_address_line1 or \
               obj.home_address_line2 != old_obj.home_address_line2 or\
               obj.home_address_line3 != old_obj.home_address_line3 or\
               obj.home_address_city != old_obj.home_address_city or\
               obj.home_address_postcode != old_obj.home_address_postcode or\
               obj.home_address_country != old_obj.home_address_country or\
               obj.home_address_phone != old_obj.home_address_phone or\
               obj.personal_email != old_obj.personal_email:
                new_address = '%s\n%s\n%s\n%s\n%s\n%s\n\nPhone: %s\nEmail: %s' %(
                    obj.home_address_line1,
                    obj.home_address_line2,
                    obj.home_address_line3,
                    obj.home_address_city,
                    obj.home_address_postcode,
                    obj.get_home_address_country_display(),
                    obj.home_address_phone,
                    obj.personal_email,
                )
                employee_detail_change_notification(obj, 'home address details', new_address)
            if obj.marital_status != old_obj.marital_status:
                employee_detail_change_notification(obj, 'marital_status', obj.get_marital_status_display())
        obj.save()

admin.site.register(Employee, EmployeeAdmin)

对此有何帮助?

1 个答案:

答案 0 :(得分:4)

管理员找不到user_name的原因是user_name字段未在类级别的自定义表单上定义。它仅在__init__中定义。当管理员检查可用字段的表单时,它只能访问该类,而不能访问已初始化的表单。因此,检查表单类并不表明user_name可用。

直接在类级别定义所有字段,而不是使用表单__init__方法。如果您不需要所有案例的所有字段,请根据需要从__init__方法中的表单字段字典中删除字段。

您也可以反向执行此操作 - 将员工信息添加到用户编辑页面?这可以按照https://docs.djangoproject.com/en/1.4/topics/auth/#adding-userprofile-fields-to-the-admin中的说明完成。

最后,您似乎已经到了可以更轻松地实现所需功能而无需使用管理员的阶段 - 而Django的管理员确实具有许多可扩展性功能,在某些时候编写您自己的实现将比使用Admin更容易。