2

我正在尝试让自定义视图在 Django 管理员中工作。我有下一个模型:

class Reseller(models.Model):
    first_name = models.CharField(max_length=64, verbose_name='First Name')
    last_name = models.CharField(max_length=64, verbose_name='Last Name')
    email = models.CharField(max_length=64, verbose_name='E-mail')
    password = models.CharField(max_length=64, blank=True, editable=False)

这就是我添加自定义按钮(重置密码)的方式。自定义视图(扩展 change_form.html)我有这个:

{% extends "admin/change_form.html" %}

{% load i18n %}

{% block object-tools %}
{% if change %}
  <ul class="object-tools">
  <li><a href="reset_password/">Reset Password</a></li>
  </ul>

{% endif %}
{% endblock %}

这就是我所拥有的admin.py

from django.conf.urls.defaults import patterns
from django.contrib import admin
from django.shortcuts import redirect
from django.shortcuts import render_to_response
from django.template import RequestContext

from myapp.resellers.models import Reseller

class ResellerAdmin(admin.ModelAdmin):
    list_display = ('id', 'first_name', 'last_name', 'email')
    list_filter = ('email')
    search_fields = ('first_name', 'last_name', 'email')
    ordering = ['-id', ]

    def get_urls(self):
        urls = super(ResellerAdmin, self).get_urls()
        my_urls = patterns('',
                           (r'(?P<id>\d+)/reset_password/$',
                            self.admin_site.admin_view(self.reset_password)),
                        )
        return my_urls + urls

    def reset_password(self, request, id):
        entry = Reseller.objects.get(pk=id)
        [...GENERATE AND SEND PASSWORD FUNCTION GOES HERE...]
        return redirect(entry)


admin.site.register(Reseller, ResellerAdmin)

当我运行此代码时,我得到下一个:argument of type 'Reseller' is not iterable. 我是 Django 的新手,所以基本上某处有一个愚蠢的错误,所以请不要投反对票:)

4

1 回答 1

2

如果您仅将模型作为参数传递给模型,则将调用redirect模型get_absolute_url()方法,您可能尚未定义该方法(请参阅文档)。
所以继续get_absolute_url()往你的 Reseller 类中添加一个方法。

于 2013-02-17T17:03:27.103 回答