2012-10-25 98 views
0

我的模式是這樣的:不能使Django的Tastypie創建資源,深深地相關資源

class LocAddress(models.Model): 
    property_number = models.CharField(max_length=8) 
    street_name = models.CharField(max_length=50) 
    postal_code = models.CharField(max_length=6) 
    city_name = models.CharField(max_length=50) 

class LluOrder(models.Model): 
    # [...] 
    address = models.ForeignKey(LocAddress, db_column='address') 

class OrderItem(models.Model): 
    # [...] 
    llu = models.ForeignKey(LluOrder, db_column='llu', null=True, blank=True) 

和資源:

class LocAddressResource(ModelResource): 
    class Meta(ModelResource.Meta): 
     queryset = LocAddress.objects.all() 


class LluOrderResource(ModelResource): 
    address = fields.ToOneField(LocAddressResource, 'address', full=True) 

    class Meta(ModelResource.Meta): 
     queryset = LluOrder.objects.all() 


class OrderItemResource(ModelResource): 
    llu = fields.ToOneField(LluOrderResource, 'llu', full=True, null=True) 

    class Meta(ModelResource.Meta): 
     queryset = OrderItem.objects.all() 

當我張貼以下數據通過`http://本地主機:8000/API/V1/OrderItem的/:

{ 
    "someotherattr": 12, 
    "llu":{ 
     "address":{ 
      "city_name":"Warszawa", 
      "postal_code":"05-600", 
      "property_number":"34", 
      "street_name":"Gr\u00f3jeckas" 
     } 
    } 
} 

我得到IntegrityError:

Traceback (most recent call last): 
    File "/Users/mmetelko/.virtualenvs/sor_interface/lib/python2.7/site-packages/tastypie/resources.py", line 192, in wrapper 
    response = callback(request, *args, **kwargs) 
    File "/Users/mmetelko/.virtualenvs/sor_interface/lib/python2.7/site-packages/tastypie/resources.py", line 397, in dispatch_list 
    return self.dispatch('list', request, **kwargs) 
    File "/Users/mmetelko/.virtualenvs/sor_interface/lib/python2.7/site-packages/tastypie/resources.py", line 427, in dispatch 
    response = method(request, **kwargs) 
    File "/Users/mmetelko/.virtualenvs/sor_interface/lib/python2.7/site-packages/tastypie/resources.py", line 1165, in post_list 
    updated_bundle = self.obj_create(bundle, request=request, **self.remove_api_resource_names(kwargs)) 
    File "/Users/mmetelko/.virtualenvs/sor_interface/lib/python2.7/site-packages/tastypie/resources.py", line 1777, in obj_create 
    self.save_related(bundle) 
    File "/Users/mmetelko/.virtualenvs/sor_interface/lib/python2.7/site-packages/tastypie/resources.py", line 1918, in save_related 
    related_obj.save() 
    File "/Users/mmetelko/.virtualenvs/sor_interface/lib/python2.7/site-packages/django/db/models/base.py", line 463, in save 
    self.save_base(using=using, force_insert=force_insert, force_update=force_update) 
    File "/Users/mmetelko/.virtualenvs/sor_interface/lib/python2.7/site-packages/django/db/models/base.py", line 551, in save_base 
    result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw) 
    File "/Users/mmetelko/.virtualenvs/sor_interface/lib/python2.7/site-packages/django/db/models/manager.py", line 203, in _insert 
    return insert_query(self.model, objs, fields, **kwargs) 
    File "/Users/mmetelko/.virtualenvs/sor_interface/lib/python2.7/site-packages/django/db/models/query.py", line 1576, in insert_query 
    return query.get_compiler(using=using).execute_sql(return_id) 
    File "/Users/mmetelko/.virtualenvs/sor_interface/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 917, in execute_sql 
    cursor.execute(sql, params) 
    File "/Users/mmetelko/.virtualenvs/sor_interface/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 52, in execute 
    return self.cursor.execute(query, args) 
IntegrityError: null value in column "address" violates not-null constraint 

我錯過了什麼嗎?我可以重寫什麼來製作嵌套LocAddress資源的tastypie?

回答

1

嘗試設置:

address = models.ForeignKey(LocAddress, db_column='address', null=True, blank=True) 
+0

但我希望它是NOT NULL – Memke

+0

NOT NULL有與DBMS值的限制做的(只是意味着該值不允許爲空)。我有一種感覺,POST被分解爲與您的數據庫分開的交易,導致可能的雞與雞蛋問題。通過適當的輸入驗證,您可以確保地址不爲空。 –