0

我的模型看起来像:

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://localhost: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资源?

4

1 回答 1

1

尝试设置:

address = models.ForeignKey(LocAddress, db_column='address', null=True, blank=True)
于 2012-10-25T19:46:43.413 回答