更改表单中的ID属性错误Django Admin

时间:2017-05-03 09:07:08

标签: python django django-admin

我想将Nation模型ID传递给Django admin change_form,以便在编辑时显示一两个按钮。我尝试将id传递给render_change_form函数并在模板中完成,但是我收到了以下错误。

'NationAdmin' object has no attribute 'nation_id'

N:B这在编辑时工作正常,但是当我想通过Django Admin仪表板添加新模型对象时会抛出此错误。

NationAdmin下的方法

  #this method is to get the id
   def change_view(self, request, nation_id=None, form_url='', extra_context=None):
        self.nation_id = nation_id
        return self.changeform_view(request, nation_id, form_url, extra_context)

 #for form
 def render_change_form(self, request, context, *args, **kwargs):
        if self.nation_id: #django is telling me this line is causing error
             nation= self.get_object(request, self.nation_id)
             context.update({'nation':nation})
        return super(NationAdmin, self).render_change_form(request, context, *args, **kwargs)

Change_form.html

   {% if not nation.is_moderated and not nation.is_rejected %} 
      <a href="{% url 'activate_moderation' nation.id %}"> <input type="button" value="{% trans 'Approve' %}" name="_approvebutton" /></a>
  {% endif %}

1 个答案:

答案 0 :(得分:0)

您可以使用hh。无需设置original

nation_id