'RelatedManager' object is not iterable Django

前端 未结 3 2073
轮回少年
轮回少年 2020-12-10 00:52

Hey i have looked around through some simliar posts here on SO but havent found anything that has solved my problem. I have the following models,

from django         


        
相关标签:
3条回答
  • 2020-12-10 00:59

    Call all() to retrieve the elements from the manager.

    {% for area in world_areas.all %}
    
    0 讨论(0)
  • 2020-12-10 01:07

    In general, it is better practice to use a values or values_list to pass data from a queryset to a template.

    world_areas = Areas.objects.select_related().all().values_list('name', 'order_in_sidebar_network', ...)
    

    Check out the Django docs for info on how to use the values function if you haven't used it before.

    0 讨论(0)
  • 2020-12-10 01:09

    I run into this issue by a reckless mistake:

     for physicalserver in task.physicalservers:
            physicalserver.relieve_task()
    

    The task.physicalservers is RelatedManager object, in my case I should get the task's physicalservers, there should add .all().

    for physicalserver in task.physicalservers.all():
    
    0 讨论(0)
提交回复
热议问题