简体   繁体   English

如何在Django ModelForm中添加额外的字段?

[英]How to add an extra field to a Django ModelForm?

So I'm working on an admin page. 所以我正在开发一个管理页面。 I'm registering the form with admin.site.register . 我正在使用admin.site.register注册表单。 And I want to add an extra field to the form, which will let me populate a TextField with a file contents. 我想在表单中添加一个额外的字段,这将允许我使用文件内容填充TextField

Therefore I need to add an extra FileInput to upload the file and populate the TextField with its contents. 因此,我需要添加一个额外的FileInput来上传文件并使用其内容填充TextField I am trying this: 我在尝试这个:

class PersonForm(forms.ModelForm):

    extra_field = forms.FileInput()

    class Meta:
        model = Person
        fields = '__all__'

but the field is not showing. 但该领域没有显示。 Any ideas? 有任何想法吗? Also I have no clue where to access the file contents and populate the TextField with that before saving the model. 此外,我不知道在保存模型之前,在何处访问文件内容并使用该文件填充TextField

Thanks in advance. 提前致谢。

My problem was in this line: 我的问题在于这一行:

extra_field = forms.FileInput()

I solved the problem changing the line to: 我解决了将线路更改为:

extra_field = forms.FileField()

Thanks to all willing to help. 感谢所有愿意提供帮助的人。

Try to do it in the constructor. 尝试在构造函数中执行此操作。

class PersonForm(forms.ModelForm):

    class Meta:
        model = Person
        fields = '__all__'

    def __init__(self, *args, **kwargs):
        super(PersonForm, self ).__init__(*args, **kwargs)
        self.fields['extra_field'] = forms.FileInput()

And since you are using the django admin, you need to change the form in the admin too. 由于您使用的是django管理员,因此您还需要在管理员中更改表单。

What you've done is ok according to the documentation, read note here - https://docs.djangoproject.com/en/2.0/topics/forms/modelforms/#overriding-the-default-fields 根据文档,你所做的就是好的,请阅读这里的注释 - https://docs.djangoproject.com/en/2.0/topics/forms/modelforms/#overriding-the-default-fields

To register it in the admin you should add something like this to your admin.py : 要在管理员中注册它,您应该在admin.py添加类似的admin.py

from django.contrib import admin
from .forms import PersonForm

@admin.register(Person)
class PersonAdmin(admin.ModelAdmin):
     form = PersonForm

Example from here - https://docs.djangoproject.com/en/2.0/ref/contrib/admin/#admin-custom-validation 这里的示例 - https://docs.djangoproject.com/en/2.0/ref/contrib/admin/#admin-custom-validation

EDIT: it is necessary to actually register custom ModelAdmin, there are two equivalent ways: using decorator, as in the example above, or use admin.site.register(Person, PersonAdmin) . 编辑:有必要实际注册自定义ModelAdmin,有两种等效的方法:使用装饰器,如上例所示,或使用admin.site.register(Person, PersonAdmin)

Documentation for ModelAdmin registration - https://docs.djangoproject.com/en/2.0/ref/contrib/admin/#the-register-decorator ModelAdmin注册的文档 - https://docs.djangoproject.com/en/2.0/ref/contrib/admin/#the-register-decorator
Registration source code - https://github.com/django/django/blob/master/django/contrib/admin/sites.py#L85 注册源代码 - https://github.com/django/django/blob/master/django/contrib/admin/sites.py#L85

Register the model in admin as 在admin中注册模型

admin.site.register(UserProfile)

where UserProfile is a OnetoOnemodel that extends django's builtin User Model then after every changes in models run 其中UserProfile是一个OnetoOnemodel,它扩展了django的内置用户模型,然后在模型中的每个更改运行之后

python manage.py makemigrations
python manage.py migrate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM