如何将初始值分配给ModelAdmin中的自定义字段?

时间:2016-11-15 07:30:13

标签: django django-forms django-admin

我正在尝试通过模型中的onetonefield扩展django用户身份验证。这是模型:

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver


class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    nim = models.CharField(max_length=10, blank=True)

    def __str__(self):
        return self.user.username

使用基本管理面板,我首先创建用户实例,然后创建配置文件。但我在管理面板上使用自定义表单,所以我可以在一个表单上创建它。这是表单外观:

Profile add form

这是forms.py

from django import forms
from django.contrib.auth.models import User

from .models import Profile  


class ProfileForm(forms.ModelForm):
    username = forms.CharField(required=False, max_length=150)
    password = forms.CharField(required=False, max_length=50, widget=forms.PasswordInput)

    class Meta:
        model = Profile
        fields = ('username', 'password', 'nim')

这是admin.py

from django.contrib import admin
from django.contrib.auth.models import User

from .forms import ProfileForm
from .models import Profile


class ProfileAdmin(admin.ModelAdmin):
    form = ProfileForm

    def save_model(self, request, obj, form, change):
        username = form.cleaned_data['username']
        password = form.cleaned_data['password']
        user = User.objects.create_user(username=username, password=password)
        user.save()
        obj.user = user
        obj.save()


admin.site.register(Profile, ProfileAdmin)

有了这一切,我可以创建配置文件实例就好了。但是当我尝试更改/编辑配置文件时,它没有显示用户名和密码字段的初始值,只有nim字段。

empty username and password field

感谢您的帮助

1 个答案:

答案 0 :(得分:4)

试试这个:

class ProfileAdmin(admin.ModelAdmin):
    form = ProfileForm
    # ...
    def get_form(self, request, obj=None, *args, **kwargs):
        form = super(ProfileAdmin, self).get_form(request, *args, **kwargs)
        #find the user via user profile forien key
        the_user = User.objects.filter(pk=Profile.user.pk)
        # Initial values
        form.base_fields['username '].initial = the_user.username
        return form

也尝试这个并看到diff :(字段仅在超级调用之后才存在)

        form.fields['username '].initial = the_user.username