English 中文(简体)
Django:压倒一切 相关的外勤人员
原标题:Django: override RelatedFieldWidgetWrapper

我想改变行政地点外国钥匙的“+”icon的表现方式。

我发现,印刷该代码的植被为 相关外勤人员WidgetWrapper,载于django/contrib/admin/widgets.py

因此,我撰写了这一类别的版本,我改变了其<代码>render/code>的功能。

但现在我如何利用它? 我指的是......在我的模型定义中,我不得不以这种方式使用<代码>formfield_overrides?

formfield_overrides = {
        models.ForeignKey: { widget : customRelatedFieldWidgetWrapper},
}

I think that this is not the right way, because that widget is not the one that manage the whole foreign key, but only the "+" icon. Am I wrong?

感谢很多。

最佳回答

你们需要为模型Admin和压倒性植被制定习惯模型。

例:

#forms.py
class CustomForm(forms.ModelForm):
    user = forms.ModelChoiceField(queryset=User.objects.all(), widget=yourCustomWidget)

class Meta:
    model = MyModel

#admin.py
class MyModelAdmin(admin.ModelAdmin):
     form = CustomForm
问题回答

我通过key骑派植被,稍有不同地对待这一变化,这样,变化就反映在所有形式上,你不会用 d子源代码 around。

我之所以这样做,是因为我正在致力于定制以下网站:http://github.com/yawd/yawd-admin”rel=“nofollow”>yawd admin,这是一个非常 n的Twitter-Bootsdorf皮肤用于行政接口。 现在,我的所有信条都受到谴责。

import django.contrib.admin.widgets

class MyRelatedFieldWidgetWrapper(django.contrib.admin.widgets.RelatedFieldWidgetWrapper):
    """
    This class is a wrapper to a given widget to add the add icon for the
    admin interface.
    """
    def render(self, name, value, *args, **kwargs):
        rel_to = self.rel.to
        info = (rel_to._meta.app_label, rel_to._meta.model_name)
        self.widget.choices = self.choices
        output = [self.widget.render(name, value, *args, **kwargs)]
        if self.can_add_related:
            related_url = reverse(
                 admin:%s_%s_add  
                % info, current_app=self.admin_site.name
            )
            output.append(
            """
            <a  href="%s"
                onclick="return showAddAnotherPopup(this);
                alt="%s">

                <i class="help icon-large icon-plus-sign"
                    id="add_id_%s"
                    data-original-title>
                </i>
            </a>""" % (related_url, _( Add Another ), name))
            return mark_safe(  .join(output))

# Monkeypatch it
django.contrib.admin.widgets.RelatedFieldWidgetWrapper = MyRelatedFieldWidgetWrapper




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签