Django REST Framework不会以PUT形式显示值 [英] Django REST Framework doesn't display value in PUT form

查看:76
本文介绍了Django REST Framework不会以PUT形式显示值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我发布了


您可以看到,键 class的值在那儿。输入元素的名称以及模型中的字段名称均为 class_name。


我在源代码中打了一下,发现表单在文件<$中呈现c $ c> renderers.py 的顺序如下:



  • 在类 BrowsableAPIRenderer 方法 get_context 创建表单并调用方法 get_rendered_html_form

  • 方法 get_rendered_html_form 调用方法 render_form_for_serializer

  • 该方法 render_form_for_serializer 调用类 HTMLFormRenderer 的方法 render 。 / li>

但是我仍然不知道在哪里进行干预以及应该更改什么。


我也尝试实施序列化器中的方法 to_internal_value ,但这仅用于反序列化,与表单呈现无关。


任何人都没有有个主意问题出在哪里,该怎么办?


UPDATE


我创建了GitHub




Yesterday I posted a question and found a solution to that problem. The solution however caused another issue.
Please take a look at the question, so I don't have to duplicate the contents.

In the Browsable API the value for 'class_name' is not displayed in the PUT form.

The rendered HTML looks like this:

<div class="form-group ">
  <label class="col-sm-2 control-label ">
    Class
  </label>

  <div class="col-sm-10">
    <input name="class_name" class="form-control" type="text">
  </div>
</div>

For the other fields it displays the value properly, for example:

<div class="form-group ">
  <label class="col-sm-2 control-label ">
    Order
  </label>

  <div class="col-sm-10">
    <input name="order" class="form-control" value="Carnivora" type="text">
  </div>
</div>

Here are screenshots illustrating the problem:

You can see that the value for the key "class" is there. The name of the input element as well the field name in the model is "class_name".

I poked around the source code and found out that the form is rendered in the file renderers.py in the following order:

  • In the class BrowsableAPIRenderer the method get_context creates the form and calls the method get_rendered_html_form.
  • The method get_rendered_html_form calls the method render_form_for_serializer.
  • The method render_form_for_serializer calls the method render of the class HTMLFormRenderer.

But I still don't know where to interfere and what should I change.

Also I tried to implement the method to_internal_value in the serializer, but this is only for the deserialization and has nothing to do with the form rendering.

Does anyone have an idea where the problem lies and what could be done?

UPDATE

I have created GitHub repo with the code. You can clone it or fork it and try to help me.
Many thanks!

解决方案

I worked out another way which seemed to be working fine for me. This was to define a class field for the serializer outside the class:

class SpeciesSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Species
        fields = (
            'url', 'id', 'canonical_name', 'slug',  'species', 'genus',
            'subfamily', 'family', 'order','class', 'phylum',
            'ncbi_id', 'ncbi_taxonomy',
        )
        read_only_fields = ('slug',)
        extra_kwargs = {
            'url': {'lookup_field': 'slug'}
        }

SpeciesSerializer._declared_fields["class"] = serializers.CharField(source="class_name")

After that both Raw Data and HTML Form look fine:

这篇关于Django REST Framework不会以PUT形式显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆