2015-09-25 89 views
1

我嘗試序列化Onetofield關係中兩個模型的字段。所以我從串行器中覆蓋更新和to_representation方法。 對於GET請求,一切正常,但是POST請求在/ personalData/覆蓋更新和to_representation方法在POST請求時拋出AttributeError

'OrderedDict' 對象有沒有屬性 '姓'

錯誤拋出一個

AttributeError的。

我的模型:

class UserData(models.Model): 
    user = models.OneToOneField(User) 

    gender = models.CharField(max_length=20, blank=True) 
    firstname = models.CharField(max_length=50, blank=True) 
    lastname = models.CharField(max_length=50, blank=True) 
    nationality = models.CharField(max_length=30, blank=True) 

class Employee(models.Model): 
    userData = models.OneToOneField(UserData) 

    birthdate = models.CharField(max_length=30, blank=True) 
    jobTitle = models.CharField(max_length=50, blank=True) 

我的序列化器類:

class EmployeePersonalDataSerializer(serializers.Serializer): 
    firstname = serializers.CharField() 
    lastname = serializers.CharField() 
    birthdate = serializers.CharField() 
    gender = serializers.CharField() 
    jobTitle = serializers.CharField() 
    nationality = serializers.CharField() 

    def update(self, instance, validated_data): 
     instance.firstname = validated_data.get('firstname', instance.firstname) 
     instance.lastname = validated_data.get('lastname', instance.lastname) 
     instance.gender = validated_data.get('gender', instance.gender) 
     instance.nationality = validated_data.get('nationality', instance.nationality) 
     instance.employee.jobTitle = validated_data.get('jobTitle', instance.employee.jobTitle) 
     instance.employee.birthdate = validated_data.get('birthdate', instance.employee.birthdate) 
     instance.employee.save() 
     instance.save() 
     return instance 


    def to_representation(self, obj): 
     return { 
        'firstname': obj.firstname, 
        'lastname': obj.lastname, 
        'birthdate': obj.employee.birthdate, 
        'gender': obj.gender, 
        'jobTitle': obj.employee.jobTitle, 
        'nationality': obj.nationality 
       } 

編輯:

完整回溯:

Traceback: 
File "/home/user/.local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 
    132.      response = wrapped_callback(request, *callback_args, **callback_kwargs) 
File "/home/user/.local/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view 
    58.   return view_func(*args, **kwargs) 
File "/home/user/.local/lib/python2.7/site-packages/django/views/generic/base.py" in view 
    71.    return self.dispatch(request, *args, **kwargs) 
File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py" in dispatch 
    466.    response = self.handle_exception(exc) 
File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py" in dispatch 
    463.    response = handler(request, *args, **kwargs) 
File "/myproject/views.py" in post 
    177.    serializer.update(instance=userData, validated_data=serializer.data) 
File "/usr/local/lib/python2.7/dist-packages/rest_framework/serializers.py" in data 
    487.   ret = super(Serializer, self).data 
File "/usr/local/lib/python2.7/dist-packages/rest_framework/serializers.py" in data 
    225.     self._data = self.to_representation(self.validated_data) 
File "/myproject/serializers.py" in to_representation 
    226.      'firstname': obj.firstname, 

Exception Type: AttributeError at /personalData/ 
Exception Value: 'OrderedDict' object has no attribute 'firstname' 
+0

請顯示完整的回溯。 – Alasdair

回答

1

FRO米DRF文檔

如果你想實現一個讀寫關係領域,還必須 實現.to_internal_value(個體經營,數據)方法。

所以,你想實現一個讀寫(POST/GET)字段,所以你需要在你的序列化器中實現.to_internal_value方法。

1

您需要使用嵌套序列化器。錯誤是說沒有提交給員工的名字,沒有。您需要一個用於Employee和UserData的序列化程序。