我正在尝试序列化(自定义)Django用户模型的实例,如下所示:
在模型.py 中:
Class Employee(User):
company = models.ForeignKey('Company')
additionalField1
additionalField2
etc....
在上面的Employee模型中,除了继承自User模型的属性外,我还使用了以下模型字段类型:CharField()、NullBooleanField()、IntegerField()、DateField()、DecimalField()
有问题的代码:
employee = Employee()
(snip large amounts of code that sets various attributes for employee)
serializers.serialize("json", [employee, ])
(顺便说一句,我安装了 WadOfStuff 的 Django 完整序列化程序插件,以防万一——但在这种情况下,我认为它应该默认为标准 Django 序列化程序,因为在这种情况下我没有使用任何完整序列化程序的功能)
尝试序列化之前的员工 __dict__ (带有一些匿名的关键字段):
{'status': u'Act', 'last_name': u'Doe', 'payFrequency': u'Mo', '_state':
<django.db.models.base.ModelState object at 0x15be890>, 'sex': u'M', 'user_ptr_id':
None, 'is_staff': False, 'isRegistered': False, 'hireDate': u'2012-08-01', 'id': None,
'date_joined': datetime.datetime(2012, 10, 25, 2, 39, 22, 793015, tzinfo=<UTC>),
'city': u'San Francisco', 'first_name': u'John', 'zip': u'94114', u'employmentType':
u'FT', 'company_id': 4, 'compType': u'S', 'is_superuser': False, 'state': u'CA',
'last_login': datetime.datetime(2012, 10, 25, 2, 39, 22, 792983, tzinfo=<UTC>),
'email': '', 'username': 'tu7wwhyskewcpheyoq4lk3i3l', 'address2': '', 'is_active':
True, 'phone': '', 'address': u'111 Cherry Lane', 'password':
'pbkdf2_sha256$10000$OAlOtfQClAV2$OC9oCe/9P5hjc4nWd1ZW6cY117PmW1pny8J41axr6mM=',
'salary': u'10833.00', 'standardHours': None, 'dob': u'1980-04-01', 'socialSecurity':
u'555555555', 'middleInitial': '', 'payRate': None}
部分回溯:
File "/usr/lib/python2.6/site-packages/django/core/serializers/__init__.py", l ine 98, in serialize
s.serialize(queryset, **options)
File "/usr/lib/python2.6/site-packages/wadofstuff/django/serializers/base.py", line 52, in serialize
self.handle_field(obj, field)
File "/usr/lib/python2.6/site-packages/wadofstuff/django/serializers/python.py ", line 71, in handle_field
self._fields[field.name] = field.value_to_string(obj)
File "/usr/lib/python2.6/site-packages/django/db/models/fields/__init__.py", l ine 722, in value_to_string
return '' if val is None else val.isoformat()
AttributeError: 'unicode' object has no attribute 'isoformat'
有什么想法可能导致错误或在这种情况下如何使序列化工作?大概有某种属性是 Serializer 不喜欢的——我怎样才能确定是哪一个?