Django admin - 中间页面上的Raw Id输入

时间:2014-07-25 23:52:57

标签: django django-admin

我在django admin(v1.6)中有一个Topic模型。我正在尝试执行一个采取查询集的操作,并使对象成为另一个主题的别名。其他主题将在intermediate page中选出。我很难理解如何将Raw ID input放到中间页面,我还没有看到任何这样的代码。

以下是我的模特

class Topic(models.Model):
    name = models.CharField(max_length=200)
    slug = models.SlugField(max_length=200, unique=True)

class Alias(models.Model):
    name = models.CharField(max_length=200)
    slug = models.SlugField(max_length=200, unique=True)
    topic = models.ForeignKey(Topic, related_name='aliases')

我的admin.py是这样的:

from django.template.response import TemplateResponse
from breakingnews.topics.models import Topic, Alias

class TopicAdmin(admin.ModelAdmin):
    def make_alias(self, request, queryset):
        if request.POST.get('post'):
            # process the queryset here
            head = request.POST.get('head')
            tail = queryset

            for topic in tail:
                a, is_new = Alias.objects.get_or_create(name=topic.name, slug=topic.slug, topic=head)
        else:
            context = {
                'title': "To what topic would you like the alias to point?",
                'queryset': queryset,
                'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME,
            }
            return TemplateResponse(request, 'admin/make_alias.html',
                context, current_app=self.admin_site.name)
    add_tag.short_description = "Make these topics an alias to another"

    ...

    actions = [
        add_tag,
    ]
admin.site.register(Topic, TopicAdmin)

make_alias.html就是这样:

{% extends "admin/base_site.html" %}
{% load i18n l10n admin_urls %}

{% block content %}
<p>{% blocktrans %}To what topic would you like the alias to point? {% endblocktrans %}</p>
<ul>{{ queryset|unordered_list }}</ul>

<form action="" method="post">{% csrf_token %}
    <div>
    <input type="text" name="head" value="" />
    {% for obj in queryset %}
    <input type="hidden" name="{{ action_checkbox_name }}" value="{{ obj.pk|unlocalize }}" />
    {% endfor %}
    <input type="hidden" name="action" value="make_alias" />
    <input type="hidden" name="post" value="yes" />
    <input type="submit" value="Confirm" />
    </div>
    <!-- <ul>{{ queryset|unordered_list }}</ul> -->

</form>
{% endblock %}

1 个答案:

答案 0 :(得分:0)

以下是让它发挥作用的原因:

将此添加到Admin.py:

from django.template.response import TemplateResponse
from breakingnews.topics.models import Topic, Alias

class AliasCreateForm(forms.ModelForm):
    parents = forms.ModelChoiceField(
        queryset=Topic.objects.all(),
        widget=ManyToManyRawIdWidget(
            Topic._meta.get_field('parents').rel, admin.site)
    )

    class Meta:
        model = Topic
        fields = ('parents',)

def make_alias(self, request, queryset):
    if request.POST.get('post'):
        # Handle the response
    else:
        formset = AliasCreateForm()
        context = {
            'title': "To what topic would you like the alias to point?",
            'queryset': queryset,
            'formset': formset,
            'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME,
            'action':'make_alias',
        }
        return TemplateResponse(request, 'admin/make_alias.html',
                                context, current_app=self.admin_site.name)
make_alias.short_description = "Make these topics an alias to another"

和make_alias.html:

{% extends "admin/base_site.html" %}
{% load i18n l10n admin_urls %}

{% block extrahead %}
<script type="text/javascript" src="/static/admin/js/admin/RelatedObjectLookups.js"></script>
{% endblock %}

{% block content %}
<form action="" method="post">{% csrf_token %}
    <ul>{{ queryset|unordered_list }}</ul>
{{ formset }}

<br /><br />
{% for obj in queryset %}
<input type="hidden" name="{{ action_checkbox_name }}" value="{{ obj.pk|unlocalize }}" />
{% endfor %}
<input type="hidden" name="action" value="{{action}}" />
<input type="hidden" name="post" value="yes" />
<input type="submit" value="Submit" />
</form>

{% endblock %}