Django deserialiser抛出KeyError

时间:2012-05-31 10:41:05

标签: django

目前,我正在使用内置的django deserialiser进行反序列化对象模型,并且我遇到了这个KeyError异常,我无法获得有关哪个密钥问题的更多信息。我不确定如何进行调试。有人有主意吗?

注意:我不能使用内置的Django调试页面。

这是我的代码的缩减版本:

try:   
        # loop over deserialised objects returned from 'deserializer'
        print "serialise 123"  
        for obj in serializers.deserialize(format, data):
            print "serialise 125"  
        return True 

    except DeserializationError:
        return -1

    except Exception as e:
        print "===================="
        print type(e)
        print "********************"
        print e.args
        print "!!!!!!!!!!!!!!!!!!!!"
        print e
        return False

这是输入XML:

<?xml version="1.0" encoding="utf-8"?>
<django-objects version="1.0">
  <object pk="D5202F00-A8C7-11E1-B68A-00219B15530E" model="contacts.company">
    <field type="CharField" name="name">Company Name</field>
    <field type="CharField" name="internal_name">ComName</field>
    <field type="CharField" name="reference">NoRef</field>
    <field to="contacts.companystatus" name="company_status" rel="ManyToOneRel">9FBC97A3-2179-11E0-AEB7-005056C00008</field>
    <field type="CharField" name="vat_number">248053</field>
    <field type="CharField" name="registration_number">43905309</field>
    <field type="FloatField" name="discount">4.0</field>
    <field type="TextField" name="notes">gfhdfghdfghdfghdfgh</field>
    <field type="TextField" name="jms_code"><None></None></field>
    <field type="TextField" name="logo"><None></None></field>
  </object>
  <object pk="D53368E1-A8C7-11E1-AF74-00219B15530E" model="contacts.address">
    <field to="contacts.company" name="company" rel="ManyToOneRel">D5202F00-A8C7-11E1-B68A-00219B15530E</field>
    <field type="CharField" name="group_name">Company Name</field>
    <field type="CharField" name="line1">Address 1</field>
    <field type="CharField" name="line2">Address 2</field>
    <field type="CharField" name="line3">Address 3</field>
    <field type="CharField" name="town">Town</field>
    <field type="CharField" name="county">Country</field>
    <field type="CharField" name="postcode">BA13 3WQ</field>
    <field type="CharField" name="country_iso">GB</field>
    <field type="CharField" name="telephone">01225555555</field>
    <field type="CharField" name="fax">01225555555</field>
    <field type="CharField" name="email">Email@Addre.ss</field>
    <field type="CharField" name="website">www.si.te</field>
    <field type="CharField" name="description">Main</field>
    <field type="CharField" name="jms_code"><None></None></field>
    <field type="CharField" name="notes">Notes</field>
  </object>
  <object pk="D53D7B00-A8C7-11E1-A34B-00219B15530E" model="contacts.contact">
    <field to="contacts.address" name="address" rel="ManyToOneRel">D53368E1-A8C7-11E1-AF74-00219B15530E</field>
    <field to="generic.title" name="title" rel="ManyToOneRel">04CD69A1-7FD4-11E0-A8A3-7071BCB8D2AB</field>
    <field type="CharField" name="forename">Forename</field>
    <field type="CharField" name="surname">Surname</field>
    <field type="CharField" name="position">Position</field>
    <field type="CharField" name="mobile">07912345064</field>
    <field type="CharField" name="direct_line">01225555555</field>
    <field type="CharField" name="email">Email@Addre.ss</field>
    <field type="IntegerField" name="origin">1</field>
    <field type="IntegerField" name="lead_source">1</field>
    <field type="TextField" name="notes">Notes</field>
    <field to="contacts.contactstatus" name="contact_status" rel="ManyToOneRel">10293BF4-217A-11E0-AEB7-005056C00008</field>
    <field to="contacts.contactmethod" name="contact_method" rel="ManyToOneRel">C240965A-2179-11E0-AEB7-005056C00008</field>
    <field type="CharField" name="jms_code"><None></None></field>
  </object>
  <object pk="89E270EE-A8D3-11E1-AC38-00219B15530E" model="contacts.supplies">
    <field to="contacts.supplier" name="supplier" rel="ManyToOneRel">43D00F61-A8C8-11E1-ACFE-00219B15530E</field>
    <field to="boughtin.boughtincontrolpanel" name="bought_in_control_panel" rel="ManyToOneRel">4</field>
  </object>
  <object pk="89F0539E-A8D3-11E1-A7EE-00219B15530E" model="contacts.supplies">
    <field to="contacts.supplier" name="supplier" rel="ManyToOneRel">43D00F61-A8C8-11E1-ACFE-00219B15530E</field>
    <field to="boughtin.boughtincontrolpanel" name="bought_in_control_panel" rel="ManyToOneRel">5</field>
  </object>
  <object pk="43D00F61-A8C8-11E1-ACFE-00219B15530E" model="contacts.supplier">
    <field to="contacts.company" name="company" rel="ManyToOneRel">D5202F00-A8C7-11E1-B68A-00219B15530E</field>
    <field to="contacts.customercategory" name="category" rel="ManyToOneRel"><None></None></field>
    <field type="CharField" name="account_number">1</field>
    <field type="CharField" name="reference">NoRef</field>
    <field type="TextField" name="notes"><None></None></field>
    <field type="DateTimeField" name="date_created">2012-05-28 14:23:08</field>
    <field type="DateTimeField" name="date_modified">2012-05-28 15:43:50</field>
    <field to="auth.user" name="user" rel="ManyToOneRel">1</field>
    <field type="CharField" name="jms_code"><None></None></field>
  </object>
</django-objects>

这是代码输出:

serialise 123
====================
<type 'exceptions.KeyError'>
********************
(u'<?xml version="1.0" encoding="utf-8"?>\n<django-objects version="1.0">\n  <object pk="D5202F00-A8C7-11E1-B68A-00219B15530E" model="contacts.company">\n    <field type="CharField" name="name">Compan
y Name</field>\n    <field type="CharField" name="internal_name">ComName</field>\n    <field type="CharField" name="reference">NoRef</field>\n    <field to="contacts.companystatus" name="company_statu
s" rel="ManyToOneRel">9FBC97A3-2179-11E0-AEB7-005056C00008</field>\n    <field type="CharField" name="vat_number">248053</field>\n    <field type="CharField" name="registration_number">43905309</field
>\n    <field type="FloatField" name="discount">4.0</field>\n    <field type="TextField" name="notes">gfhdfghdfghdfghdfgh</field>\n    <field type="TextField" name="jms_code"><None></None></field>\n
  <field type="TextField" name="logo"><None></None></field>\n  </object>\n  <object pk="D53368E1-A8C7-11E1-AF74-00219B15530E" model="contacts.address">\n    <field to="contacts.company" name="company"
 rel="ManyToOneRel">D5202F00-A8C7-11E1-B68A-00219B15530E</field>\n    <field type="CharField" name="group_name">Company Name</field>\n    <field type="CharField" name="line1">Address 1</field>\n    <f
ield type="CharField" name="line2">Address 2</field>\n    <field type="CharField" name="line3">Address 3</field>\n    <field type="CharField" name="town">Town</field>\n    <field type="CharField" name
="county">Country</field>\n    <field type="CharField" name="postcode">BA13 3WQ</field>\n    <field type="CharField" name="country_iso">GB</field>\n    <field type="CharField" name="telephone">0122555
5555</field>\n    <field type="CharField" name="fax">01225555555</field>\n    <field type="CharField" name="email">Email@Addre.ss</field>\n    <field type="CharField" name="website">www.si.te</field>\
n    <field type="CharField" name="description">Main</field>\n    <field type="CharField" name="jms_code"><None></None></field>\n    <field type="CharField" name="notes">Notes</field>\n  </object>\n
<object pk="D53D7B00-A8C7-11E1-A34B-00219B15530E" model="contacts.contact">\n    <field to="contacts.address" name="address" rel="ManyToOneRel">D53368E1-A8C7-11E1-AF74-00219B15530E</field>\n    <field
 to="generic.title" name="title" rel="ManyToOneRel">04CD69A1-7FD4-11E0-A8A3-7071BCB8D2AB</field>\n    <field type="CharField" name="forename">Forename</field>\n    <field type="CharField" name="surnam
e">Surname</field>\n    <field type="CharField" name="position">Position</field>\n    <field type="CharField" name="mobile">07912345064</field>\n    <field type="CharField" name="direct_line">01225555
555</field>\n    <field type="CharField" name="email">Email@Addre.ss</field>\n    <field type="IntegerField" name="origin">1</field>\n    <field type="IntegerField" name="lead_source">1</field>\n    <
field type="TextField" name="notes">Notes</field>\n    <field to="contacts.contactstatus" name="contact_status" rel="ManyToOneRel">10293BF4-217A-11E0-AEB7-005056C00008</field>\n    <field to="contacts
.contactmethod" name="contact_method" rel="ManyToOneRel">C240965A-2179-11E0-AEB7-005056C00008</field>\n    <field type="CharField" name="jms_code"><None></None></field>\n  </object>\n  <object pk="89E
270EE-A8D3-11E1-AC38-00219B15530E" model="contacts.supplies">\n    <field to="contacts.supplier" name="supplier" rel="ManyToOneRel">43D00F61-A8C8-11E1-ACFE-00219B15530E</field>\n    <field to="boughti
n.boughtincontrolpanel" name="bought_in_control_panel" rel="ManyToOneRel">4</field>\n  </object>\n  <object pk="89F0539E-A8D3-11E1-A7EE-00219B15530E" model="contacts.supplies">\n    <field to="contact
s.supplier" name="supplier" rel="ManyToOneRel">43D00F61-A8C8-11E1-ACFE-00219B15530E</field>\n    <field to="boughtin.boughtincontrolpanel" name="bought_in_control_panel" rel="ManyToOneRel">5</field>\n
  </object>\n  <object pk="43D00F61-A8C8-11E1-ACFE-00219B15530E" model="contacts.supplier">\n    <field to="contacts.company" name="company" rel="ManyToOneRel">D5202F00-A8C7-11E1-B68A-00219B15530E</fi
eld>\n    <field to="contacts.customercategory" name="category" rel="ManyToOneRel"><None></None></field>\n    <field type="CharField" name="account_number">1</field>\n    <field type="CharField" name=
"reference">NoRef</field>\n    <field type="TextField" name="notes"><None></None></field>\n    <field type="DateTimeField" name="date_created">2012-05-28 14:23:08</field>\n    <field type="DateTimeFie
ld" name="date_modified">2012-05-28 15:43:50</field>\n    <field to="auth.user" name="user" rel="ManyToOneRel">1</field>\n    <field type="CharField" name="jms_code"><None></None></field>\n  </object>
\n</django-objects>',)
!!!!!!!!!!!!!!!!!!!!
u'<?xml version="1.0" encoding="utf-8"?>\n<django-objects version="1.0">\n  <object pk="D5202F00-A8C7-11E1-B68A-00219B15530E" model="contacts.company">\n    <field type="CharField" name="name">Company
 Name</field>\n    <field type="CharField" name="internal_name">ComName</field>\n    <field type="CharField" name="reference">NoRef</field>\n    <field to="contacts.companystatus" name="company_status
" rel="ManyToOneRel">9FBC97A3-2179-11E0-AEB7-005056C00008</field>\n    <field type="CharField" name="vat_number">248053</field>\n    <field type="CharField" name="registration_number">43905309</field>
\n    <field type="FloatField" name="discount">4.0</field>\n    <field type="TextField" name="notes">gfhdfghdfghdfghdfgh</field>\n    <field type="TextField" name="jms_code"><None></None></field>\n
 <field type="TextField" name="logo"><None></None></field>\n  </object>\n  <object pk="D53368E1-A8C7-11E1-AF74-00219B15530E" model="contacts.address">\n    <field to="contacts.company" name="company"
rel="ManyToOneRel">D5202F00-A8C7-11E1-B68A-00219B15530E</field>\n    <field type="CharField" name="group_name">Company Name</field>\n    <field type="CharField" name="line1">Address 1</field>\n    <fi
eld type="CharField" name="line2">Address 2</field>\n    <field type="CharField" name="line3">Address 3</field>\n    <field type="CharField" name="town">Town</field>\n    <field type="CharField" name=
"county">Country</field>\n    <field type="CharField" name="postcode">BA13 3WQ</field>\n    <field type="CharField" name="country_iso">GB</field>\n    <field type="CharField" name="telephone">01225555
555</field>\n    <field type="CharField" name="fax">01225555555</field>\n    <field type="CharField" name="email">Email@Addre.ss</field>\n    <field type="CharField" name="website">www.si.te</field>\n
    <field type="CharField" name="description">Main</field>\n    <field type="CharField" name="jms_code"><None></None></field>\n    <field type="CharField" name="notes">Notes</field>\n  </object>\n  <
object pk="D53D7B00-A8C7-11E1-A34B-00219B15530E" model="contacts.contact">\n    <field to="contacts.address" name="address" rel="ManyToOneRel">D53368E1-A8C7-11E1-AF74-00219B15530E</field>\n    <field
to="generic.title" name="title" rel="ManyToOneRel">04CD69A1-7FD4-11E0-A8A3-7071BCB8D2AB</field>\n    <field type="CharField" name="forename">Forename</field>\n    <field type="CharField" name="surname
">Surname</field>\n    <field type="CharField" name="position">Position</field>\n    <field type="CharField" name="mobile">07912345064</field>\n    <field type="CharField" name="direct_line">012255555
55</field>\n    <field type="CharField" name="email">Email@Addre.ss</field>\n    <field type="IntegerField" name="origin">1</field>\n    <field type="IntegerField" name="lead_source">1</field>\n    <f
ield type="TextField" name="notes">Notes</field>\n    <field to="contacts.contactstatus" name="contact_status" rel="ManyToOneRel">10293BF4-217A-11E0-AEB7-005056C00008</field>\n    <field to="contacts.
contactmethod" name="contact_method" rel="ManyToOneRel">C240965A-2179-11E0-AEB7-005056C00008</field>\n    <field type="CharField" name="jms_code"><None></None></field>\n  </object>\n  <object pk="89E2
70EE-A8D3-11E1-AC38-00219B15530E" model="contacts.supplies">\n    <field to="contacts.supplier" name="supplier" rel="ManyToOneRel">43D00F61-A8C8-11E1-ACFE-00219B15530E</field>\n    <field to="boughtin
.boughtincontrolpanel" name="bought_in_control_panel" rel="ManyToOneRel">4</field>\n  </object>\n  <object pk="89F0539E-A8D3-11E1-A7EE-00219B15530E" model="contacts.supplies">\n    <field to="contacts
.supplier" name="supplier" rel="ManyToOneRel">43D00F61-A8C8-11E1-ACFE-00219B15530E</field>\n    <field to="boughtin.boughtincontrolpanel" name="bought_in_control_panel" rel="ManyToOneRel">5</field>\n
 </object>\n  <object pk="43D00F61-A8C8-11E1-ACFE-00219B15530E" model="contacts.supplier">\n    <field to="contacts.company" name="company" rel="ManyToOneRel">D5202F00-A8C7-11E1-B68A-00219B15530E</fie
ld>\n    <field to="contacts.customercategory" name="category" rel="ManyToOneRel"><None></None></field>\n    <field type="CharField" name="account_number">1</field>\n    <field type="CharField" name="
reference">NoRef</field>\n    <field type="TextField" name="notes"><None></None></field>\n    <field type="DateTimeField" name="date_created">2012-05-28 14:23:08</field>\n    <field type="DateTimeFiel
d" name="date_modified">2012-05-28 15:43:50</field>\n    <field to="auth.user" name="user" rel="ManyToOneRel">1</field>\n    <field type="CharField" name="jms_code"><None></None></field>\n  </object>\
n</django-objects>'

1 个答案:

答案 0 :(得分:1)

您的异常处理代码实际上是阻止您获得有用的调试信息的原因。只需删除所有这些无用的异常处理,您将获得完整的错误消息和一个很好的回溯。