如何为Django管理模型添加实例表单设置初始数据? [英] How to set initial data for Django admin model add instance form?

查看:157
本文介绍了如何为Django管理模型添加实例表单设置初始数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在显示表单之前,如何在自动生成的表单中设置字段的初始值以添加Django模型实例?我正在使用Django 1.3.1.

How can I set an initial value of a field in the automatically generated form for adding a Django model instance, before the form is displayed? I am using Django 1.3.1.

我的模型如下:

class Foo(models.Model):
  title = models.CharField(max_length=50)
  description = models.TextField()

并且当前的管理表单真的没什么特别的

and the current admin form is really nothing special

class FooAdmin(admin.ModelAdmin):
  ordering = ('title',)

当我使用管理页面添加Foo的新实例时,我得到一个漂亮的表格,其中包含用于标题和描述的空字段.我想要的是描述字段设置有我通过调用函数获得的模板.

When I use the admin page to add a new instance of Foo, I get a nice form with empty fields for title and description. What I would like is that the description field is set with a template that I obtain by calling a function.

我目前到达那里的最佳尝试是

My current best attempt at getting there is this:

def get_default_content():
  return 'this is a template for a Foo description'

class FooAdminForm(django.forms.ModelForm):

  class Meta:
      model = Foo

  def __init__(self, *args, **kwargs):
      kwargs['initial'].update({'description': get_default_content()})
      super(FooAdminForm, self).__init__(self, *args, **kwargs)

class FooAdmin(admin.ModelAdmin):
  ordering = ('title',)
  form = FooAdminForm

但是,如果我尝试此操作,则会出现此Django错误:

but if I try this I get this Django error:

AttributeError at /admin/bar/foo/add/ 
   'FooForm' object has no attribute 'get'
Request Method: GET
Request URL:    http://localhost:8000/admin/bar/foo/add/
Django Version: 1.3.1
Exception Type: AttributeError
Exception Value:    'FooForm' object has no attribute 'get'
Exception Location: /www/django-site/venv/lib/python2.6/site-packages/django/forms/widgets.py in value_from_datadict, line 178

我不知道这里有什么问题,应该怎么做才能使其正常工作.我对此错误也感到奇怪(除了我看到的事实之外),我的代码中根本没有FooForm?

I don't know what is wrong here, and what I should do to make it work. What I also find strange about this error (apart from the fact that I see it at all) is that there is no FooForm in my code at all?

推荐答案

您需要在__init__方法定义中包括self作为第一个参数,但在调用超类的方法时不应将其包括在内. /p>

You need to include self as the first argument in your __init__ method definition, but should not include it when you call the superclass' method.

def __init__(self, *args, **kwargs):
    # We can't assume that kwargs['initial'] exists! 
    if 'initial' not in kwargs:
        kwargs['initial'] = {}
    kwargs['initial'].update({'description': get_default_content()})
    super(FooAdminForm, self).__init__(*args, **kwargs)

话虽如此,模型字段可以为其 default ,因此您可能根本不必定义自定义管理表单.

Having said that, a model field can take a callable for its default, so you may not have to define a custom admin form at all.

class Foo(models.Model):
    title = models.CharField(max_length=50)
    description = models.TextField(default=get_default_content)

这篇关于如何为Django管理模型添加实例表单设置初始数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆