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 に答える