简体   繁体   中英

DJANGO: How to Render swagger-codegen model objects instead of database model object

I have an existing Django project in which I am trying to separate the backend and frontend of an existing webapp. The idea is to expose the backend functions as a rest api and then also build a webapp that allows users to interact with the api functionality in a graphical way. The api exposes a swagger/openapi document and I have used swagger codegen to generate a client to intereact with that.

as a proof of concept I am trying to reimplement a view that used database object directly with one that calls the api to do the same functionality

here is the original view

def load_mapping_metadata_only_operations(request):
    mapping_operation_list = MappingOperation.objects.using('metadata')
    print(mapping_operation_list[1])

    context = {
        'mapping_operation_list': mapping_operation_list,
        'transformation_list': [],
        'source_table_list': [],
        'destination_table_list': [],
           }
    x = render(request, 'migration_core/migation_tool.html', context)
    print(x.content)
    return x

and here is the view that I am now trying to replace it with.

def load_mapping_metadata_only_operations(request):
    try:
        mapping_operation_response = api_instance.find_models_by_name()
    except ApiException as e:
    print("Exception when calling MappingApi->find_models_by_name: %s\n" % e)
    print(mapping_operation_response[1])
    api_response_dict =
    context = {
        'mapping_operation_list': mapping_operation_response,
        'transformation_list': [],
        'source_table_list': [],
        'destination_table_list': [],
               }
    x = render(request, 'migration_core/migation_tool.html', context)
    print(x.content)
    return x

this is the template that the list is sent to:

 <li class="nav-item">
<select class="selectpicker mr-sm-2" data-width="fit" data-live-search="true" title="MappingOperations" onchange="location = this.value">
{% if mapping_operation_list %}
{% for mapping_operation in mapping_operation_list.all %}
    <option value="{% url 'load_mapping_metadata_by_operation' mapping_operation.id %}"
        {% if mapping_operation_id %}

so, in the old situation the mapping_operation_list is a QuerySet and that renders just fine, in the new situation, the mapping_operation_response is a list of MappingOperation object, these objects are not the same class as the model used by the original view, they are object of a class generated with swagger-codegen. The class does have the name attribute This object is not rendered, no data is shown at all.

several print statements have confirmed to me that data is actually being retrieved in the new situtation, so that is not the problem, the template just won't render the result.

I do not understand why, there is nothing about this template that should care what class the data is in before it is sent to the template. I think I am missing something about Django that causes the data just not to be sent.

Is it possible to render objects that are not part of the model used by django? Specifically swagger-codegen generated objects? What is the best way of doing so, should I create a dict from these objects?

I have found the problem in the meantime, my template use.all (I thought this was just template language to get all items in a set/list). IT turns out that it is a method specific to QuerySet

removing the.all in

{% for mapping_operation in mapping_operation_list.all %}

will display the data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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