2013-09-29 90 views
0

我一直有麻煩理解爲什麼傳遞到field_to_native()的對象(obj)從Nonetype更改爲正確的對象,一旦我更改屬性...Django REST框架:對象是沒有隻有一個屬性,但沒有其他

這裏是原來的問題:Splitting model instance for serializer into 3 different fields

從計算器mariodev幫我在原來的問題,但一個奇怪的錯誤我們都想不通:

在此處,它似乎有問題的代碼:

COORD = dict(x=0, y=1, z=2) 
class CoordField(serializers.Field): 
    def field_to_native(self, obj, field_name): 
     #retrieve and split coords 
     coor = obj.xyz.split('x') 
     return int(coor[COORD[field_name]]) 

class NoteSerializer(serializers.ModelSerializer): 
    owner = serializers.Field(source='owner.username') 
    firstname = serializers.Field(source='owner.first_name') 
    lastname = serializers.Field(source='owner.last_name') 
    x = CoordField() 
    y = CoordField() 
    z = CoordField() 


class Meta: 
    model = Note 
    fields = ('id','owner','firstname','lastname','text','color','time','x', 'y', 'z') 

xyz是模型中的一個實例。根據追溯,當我做obj.xyz時,obj = None。

古怪,如果我做obj.color,obj的正確返回(注:SomeUser的)

我不明白是怎麼OBJ可以通過改變屬性只是改變。

我的超越之處在於JSON數據'x''y'和'z'被傳遞到視圖,而我的div盒獲得正確的左,頂部和z索引CSS數據。如果這樣做,爲什麼我會收到錯誤?

如果出現錯誤,爲什麼數據仍然通過?

style="left: 343px; top: 110px; z-index: 3;" 

正如您所看到的,x,y,z DID通過。

任何啓蒙將是美好的!謝謝一堆!

下面是在文本視圖回溯:

Traceback: 
File "/home1/thecupno/python2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 
    140.      response = response.render() 
File "/home1/thecupno/python2.7/lib/python2.7/site-packages/django/template/response.py" in render 
    105.    self.content = self.rendered_content 
File "/home1/thecupno/python2.7/lib/python2.7/site-packages/rest_framework/response.py" in rendered_content 
    59.   ret = renderer.render(self.data, media_type, context) 
File "/home1/thecupno/python2.7/lib/python2.7/site-packages/rest_framework/renderers.py" in render 
    582.   post_form = self.get_rendered_html_form(view, 'POST', request) 
File "/home1/thecupno/python2.7/lib/python2.7/site-packages/rest_framework/renderers.py" in get_rendered_html_form 
    485.    data = serializer.data 
File "/home1/thecupno/python2.7/lib/python2.7/site-packages/rest_framework/serializers.py" in data 
    510.     self._data = self.to_native(obj) 
File "/home1/thecupno/python2.7/lib/python2.7/site-packages/rest_framework/serializers.py" in to_native 
    309.    value = field.field_to_native(obj, field_name) 
File "/home1/thecupno/django/notes/desk/serializers.py" in field_to_native 
    10.   coor = obj.xyz#.split('x')  <-- I commented out .split, and the problem still persists. 
Exception Type: AttributeError at /desk/restnote/ 
Exception Value: 'NoneType' object has no attribute 'xyz' 
+0

您能否提供完整的失敗案例?目前有點難以理解發生了什麼 - 例如,如果您獲得了成功的響應,那麼追溯來自哪裏? – Hamish

+0

好吧,我可以從我的HTML頁面上的getJSON中獲取'x','y','z'的JSON數據。奇怪的是,當我訪問REST Web API網址時,它顯示「Nonetype沒有屬性xyz」,當它清楚地將xyz處理爲separete'x''y''z'JSON時。 看來,如果它正在處理2次,一個與對象,一個沒有。如果我使用另一個屬性(如顏色),該對象將正確返回。 因爲我有一個服務器錯誤,我什麼也做不了。奇怪的是,即使有服務器錯誤,它仍然給我GET數據... –

+0

基本上,如果我做obj.xyz,用Django中的DEBUG工具,它說obj = None。當我做obj.color時,它會以<注意:someusername>正確返回。 然而,在我的HTML頁面上的getJSON()中,我仍然得到了obj.xyz的JSON數據。什麼? –

回答

3

我已經解決我自己的問題。 解決方案是確保序列化程序在對象爲空的情況下返回None。

這裏的corretion頂端代碼部分:

COORD = dict(x=0, y=1, z=2) 
class CoordField(serializers.Field): 
def field_to_native(self, obj, field_name): 
    if obj is None: 
     return None 
    else: 
     #retrieve and split coords 
     coor = obj.xyz.split('x') 
     return int(coor[COORD[field_name]]) 

希望這有助於傢伙Django的程序員!

相關問題