class Message(models.Model):
date = models.DateField()
user = models.ForeignKey(UserEx)
groups = models.ManyToManyField(Group)
main_category = models.ForeignKey(MainCategories)
sub_category = models.ForeignKey(SubCategories)
我想要动态填充一些字段。所以,我对此有一些疑问。
嗯,最后一个问题 - 也许创建自己的管理面板会更容易?但是在管理面板中考虑实现排序和显示事件真是太可怕了,这真的很舒服。
答案 0 :(得分:3)
你可以通过覆盖ModelForm来实现:
class MessageAdminForm(forms.ModelForm):
def __init__(self, *arg, **kwargs):
super(MessageAdminForm, self).__init__(*args, **kwargs)
# set initial values this way
self.initial['user'] = User.objects.get(username='bob')
# set choices this way
self.fields['groups'].choices = [(g.id, g) for g in Groups.objects.all()]
class MessageAdmin(admin.ModelAdmin):
form = MessageAdminForm
# other stuff here