TypeError对象不可迭代 [英] TypeError object is not iterable

查看:252
本文介绍了TypeError对象不可迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在Django模板中循环变量时出现以下错误.有问题的变量是在我的DetailView子类中指定的模型的相关对象:

I am getting the following error when trying to loop over a variable in my Django templates. The variable in question is the related object of the model specified in my DetailView subclass:

/p/en/applicants/50771459778/

TypeError at /en/applicants/50771459778/

TypeError

家庭成员"对象不可迭代

'Householdmember' object is not iterable

这是我的models.py文件:

class Applicant(models.Model):
    user              = models.ForeignKey(User, editable=False)
    bank_card_number  = models.CharField(_('Bank card number'),max_length=50, unique=True)
    site_of_interview = models.IntegerField(_('Site of interview'), choices = SITE_CHOICES, default=TIRANA, blank=False)
    housenumber       = models.CharField(_('House Number'),max_length=8)
    address_line1     = models.CharField(_('Address line 1'),max_length=50)
    address_line2     = models.CharField(_('Apt #'),max_length=50,blank=True) 
    municipality      = models.CharField(_('Municipality/commune'),max_length=25)
    district          = models.CharField(_('District'),max_length=25,blank=True)
    urban             = models.IntegerField(_('Area (urban/rural)'), choices = AREA_CHOICES, blank=False)
    postal            = models.CharField(_('Postal code'),max_length=25,blank=True) 

class Householdmember(models.Model):
    applicant         = models.ForeignKey(Applicant)
    first_name        = models.CharField(_('First name'),max_length=50,blank=False)
    middle_name       = models.CharField(_('Middle name'),max_length=50,blank=True) 
    last_name         = models.CharField(_('Last name'),max_length=50,blank=False)
    national_id       = models.CharField(_('National ID'),max_length=50,blank=False, unique=True)
    male              = models.IntegerField(_('Gender'), choices = GENDER_CHOICES, blank=False)
    date_of_birth     = models.DateField()
    rel_to_head       = models.IntegerField(_('Gender'), choices = RELTOHEAD_CHOICES, blank=False)
    disability        = models.IntegerField(_('Is disabled?'), choices = YESNO_CHOICES, blank=False)
    created_at        = models.DateTimeField(auto_now_add = True)
    updated_at        = models.DateTimeField(auto_now = True)

这是我的urls.py文件:

class ListViewApplicants(ListView):
    paginate_by = 100
    def get_queryset(self):
        return Applicant.objects.all()

class DetailViewUnmask(DetailView):
    def get_object(self):
        return self.get_queryset().get(pk=mask_toggle(self.kwargs.get("pk_masked")))

urlpatterns = patterns('',
    url(r'^$',
        login_required(ListViewApplicants.as_view( 
                            template_name='applicants/index.html',
                            #context_object_name='form',
                            )),
        name='index'),
    url(r'^(?P<pk_masked>\d+)/$',
        login_required(DetailViewUnmask.as_view( model=Applicant,
                            template_name='applicants/detail.html'
                            )), 
        name='detail'),

这是我模板detail.html的相关部分:

Here is the relevant part of my template, detail.html:

<h2>Household members</h2>
<table class="package_detail">
    <tr>
        {% include "applicants/householdmember_heading_snippet.html" %}
    </tr>

    {% for householdmember in applicant.householdmember_set.all %}
    <tr>

        {% for field in householdmember %}
            <td>{{ field }}</td>
        {% endfor %}
        <!--
        <td>{{ householdmember.first_name }}</td>
        <td>{{ householdmember.middle_name  }}</td>
        <td>{{ householdmember.last_name  }}</td>
        <td>{{ householdmember.national_id  }}</td>
        <td>{{ householdmember.get_male_display }}</td>
        <td>{{ householdmember.date_of_birth }}</td>
        <td>{{ householdmember.get_rel_to_head_display }}</td>
        <td>{{ householdmember.get_disability_display }}</td>
        -->
    </tr>
    {% endfor %}
</table>

被注释掉的部分(即<!-- -->标记之间的部分)有效,这使我认为我应该能够遍历householdmember变量.但是,当我尝试这样做时,它不起作用-我只是得到上面的TypeError.

The part that is commented out (i.e. the part in between the <!-- --> tags) works, which leads me to think that I should be able to iterate over the householdmember variable. But when I try to do so, it doesn't work - I just get the TypeError above.

我已经在stackoverflow.com上搜索了一个答案,但是我能找到的最接近的答案是这个答案:

I have searched stackoverflow.com exentsively for an answer, but the closest answer I could find is this one: django how to loop through the context object passed back by a generic detailview?, but it does not solve my problem, I think because I'm using class based views.

非常感谢您的帮助.谢谢!

Would greatly appreciate any help. Thanks!

推荐答案

您无法遍历模型实例. 我建议您使用注释的代码.

You can't iter over a model instance. I recommend you use your commented code.

如果您仍然想使用forloop,则可以添加以下代码:

If you still want to use a forloop, maybe you can add this code:

class Householdmember(models.Model):
    # all yuur fields...

    def __iter__(self):
        return return [field.value_to_string(self) for field in Householdmember._meta.fields]

但是,没有人推荐

那更好:

class Householdmember(models.Model):
    # all yuur fields...

    def __iter__(self):
        return [ self.first_name, 
                 self.middle_name, 
                 self.last_name, 
                 self.national_id, 
                 self.get_male_display, 
                 self.date_of_birth, 
                 self.get_rel_to_head_display, 
                 self.get_disability_display ] 

这篇关于TypeError对象不可迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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