姜戈“乡土风味”在管理中选择小部件

发布于 2024-12-11 10:40:40 字数 378 浏览 0 评论 0原文

我想使用 in_.forms.INStateSelect行政 。

我应该在模型中添加什么文件?我怎样才能让它在 admin 中显示一个小部件?

编辑

我正在尝试这个

from django.contrib.localflavor.in_.forms import INStateSelect
state = INStateSelect()

谢谢!

I want to use in_.forms.INStateSelect in admin .

what filed should I add in model ? and how can I make it to show a widget in admin ?

Edit

I am trying this

from django.contrib.localflavor.in_.forms import INStateSelect
state = INStateSelect()

Thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

写给空气的情书 2024-12-18 10:40:40

你在哪里尝试该代码?需要将小部件附加到字段...

这是您可以执行此操作的一种方法:

class MyAdmin(admin.ModelAdmin):
    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name == 'my_state_field':
            kwargs['widget'] = INStateSelect()
        return super(MyAdmin, self).formfield_for_dbfield(db_field, **kwargs)

另一种方法是覆盖管理表单并使用 INStateField

class MyForm(forms.ModelForm):
    class Meta:
        model = MyModel

    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        self.fields['myfield'] = INStateField()


class MyAdmin(admin.ModelAdmin):
    form = MyForm

Where are you trying that code? A widget needs to be attached to a field...

Here's one way you could do this:

class MyAdmin(admin.ModelAdmin):
    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name == 'my_state_field':
            kwargs['widget'] = INStateSelect()
        return super(MyAdmin, self).formfield_for_dbfield(db_field, **kwargs)

Another would be to override the admin form and use the INStateField

class MyForm(forms.ModelForm):
    class Meta:
        model = MyModel

    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        self.fields['myfield'] = INStateField()


class MyAdmin(admin.ModelAdmin):
    form = MyForm
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文