如何从管理中动态排除非模型字段?

发布于 2024-08-20 06:02:44 字数 1726 浏览 4 评论 0原文

我已向 ModelForm 添加了一个自定义字段,以供在管理中使用:

class MyModel(models.Model):
    name = models.CharField(max_length=64)
     ...etc...

class MyModelAdminForm(forms.ModelForm):
    dynamicfield = forms.IntegerField()

    def __init__(self, *args, **kwargs):
        super(MyModelAdminForm, self).__init__(*args, **kwargs)
        if 'instance' in kwargs:
            #remove dynamicfield from the form, somehow?

    def save(self, *args, **kwargs):
        #do something with dynamicfield
        super(MyModelAdminForm, self).save(*args, **kwargs)

    class Meta:
        model = MyModel

class MyModelAdmin(admin.ModelAdmin):
    form = MyModelAdminForm

admin.site.register(MyModel, MyModelAdmin)

该字段的目的是收集初始对象创建期间所需的附加数据。因此,它应该只在创建新对象时出现(检查“实例”)。

我遇到的问题是在编辑现有对象时使该字段消失。我已经尝试过:

self.fields['dynamicfield'].widget = self.fields['dynamicfield'].hidden_widget()

但这只会删除文本输入字段,而不是标签。

我也尝试过这些的变体(也在 super() 调用之前使用 base_fields 代替字段):

del self.fields['dynamicfield'] #yields error:

TemplateSyntaxError at /admin/db/myapp/mymodel/6/
Caught an exception while rendering: Key 'dynamicfield' not found in Form


self.fields['dynamicfield'].hidden = True #Does nothing.

self.fields['dynamicfield'] = None #yields error:

TemplateSyntaxError at /admin/db/catalog/product/6/
Caught an exception while rendering: 'NoneType' object has no attribute 'label'    

在调用 super() 之前执行这些操作也尝试过,但也失败:

del self.base_fields['dynamicfield'] #yields error:
TemplateSyntaxError at /admin/db/myapp/mymodel/6/
Caught an exception while rendering: Key 'dynamicfield' not found in Form

I've added a custom field to a ModelForm for use in the admin:

class MyModel(models.Model):
    name = models.CharField(max_length=64)
     ...etc...

class MyModelAdminForm(forms.ModelForm):
    dynamicfield = forms.IntegerField()

    def __init__(self, *args, **kwargs):
        super(MyModelAdminForm, self).__init__(*args, **kwargs)
        if 'instance' in kwargs:
            #remove dynamicfield from the form, somehow?

    def save(self, *args, **kwargs):
        #do something with dynamicfield
        super(MyModelAdminForm, self).save(*args, **kwargs)

    class Meta:
        model = MyModel

class MyModelAdmin(admin.ModelAdmin):
    form = MyModelAdminForm

admin.site.register(MyModel, MyModelAdmin)

The point of this field is to collect additional data needed during initial object creation. Therefore it should only appear when a new object is being created (the check for 'instance').

The problem I'm having is making the field go away when editing an existing object. I've tried:

self.fields['dynamicfield'].widget = self.fields['dynamicfield'].hidden_widget()

but that only gets rid of the text input field, not the label.

I've also tried variations on these (also using base_fields in place of fields, before the super() call):

del self.fields['dynamicfield'] #yields error:

TemplateSyntaxError at /admin/db/myapp/mymodel/6/
Caught an exception while rendering: Key 'dynamicfield' not found in Form


self.fields['dynamicfield'].hidden = True #Does nothing.

self.fields['dynamicfield'] = None #yields error:

TemplateSyntaxError at /admin/db/catalog/product/6/
Caught an exception while rendering: 'NoneType' object has no attribute 'label'    

Doing these before the call to super() also tried, but also fail:

del self.base_fields['dynamicfield'] #yields error:
TemplateSyntaxError at /admin/db/myapp/mymodel/6/
Caught an exception while rendering: Key 'dynamicfield' not found in Form

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

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

发布评论

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

评论(2

情深如许 2024-08-27 06:02:44

试试这个:

    def __init__(self, *args, **kwargs):

        if 'instance' in kwargs:
            del self.base_fields['dynamicfield']
        super(MyModelAdminForm, self).__init__(*args, **kwargs)

Try this:

    def __init__(self, *args, **kwargs):

        if 'instance' in kwargs:
            del self.base_fields['dynamicfield']
        super(MyModelAdminForm, self).__init__(*args, **kwargs)
左耳近心 2024-08-27 06:02:44

如果删除不起作用(我不知道为什么)动态添加怎么办?

def __init__(self, *args, **kwargs):
    super(MyModelAdminForm, self).__init__(*args, **kwargs)
    if 'instance' not in kwargs:
       self.fields['dynamicfield'] = forms.IntegerField()

这里还有一个关于如何创建动态表单。 (我认为他甚至是创建 Django 的人?)

If the removal does not work (which I don't know why) what about dynamic adding?

def __init__(self, *args, **kwargs):
    super(MyModelAdminForm, self).__init__(*args, **kwargs)
    if 'instance' not in kwargs:
       self.fields['dynamicfield'] = forms.IntegerField()

Here is also a link on how to create dynamic forms. (I think it is even the guy who started Django?)

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