使用外键更新资源中的Tastypie

时间:2012-10-15 14:21:21

标签: django-models foreign-keys tastypie

我有一个问题(不支持的**或pow()的操作数类型:'Decimal'和'NoneType'),其中涉及外键约束的资源。这是一段代码(联系人属于部门和客户):

class Client(models.Model):
    name = models.CharField(max_length=100, unique=True)
    address = models.CharField(max_length=100, null=True)

class Department(models.Model):
    name = models.CharField(max_length=50, unique=True)

class Contact(models.Model):
    name = models.CharField(max_length=50, null=True)
    phone = models.CharField(max_length=20, null=True)
    email = models.EmailField(max_length=50, null=True)
    department = models.ForeignKey(Department)
    client = models.ForeignKey(Client)

class BaseResource(ModelResource):
    class Meta:
        allowed_methods = ['get','post', 'put', 'delete', 'patch']
            authorization = Authorization()
            abstract = True

class ClientResource(BaseResource):
    class Meta(BaseResource.Meta):
        queryset = Client.objects.all()
        resource_name = 'client'

class DepartmentResource(BaseResource):
    class Meta(BaseResource.Meta):
        queryset = Department.objects.all()
        resource_name = 'department'

class ContactResource(BaseResource):
    department = fields.ToOneField(DepartmentResource, 'department', full=True)
    client = fields.ToOneField(ClientResource, 'client', full=False)
    class Meta(BaseResource.Meta):
        queryset = Contact.objects.all()
        resource_name = 'contact'

当我尝试更新部门时,我没有遇到任何问题。我这样做:

curl -v -XPATCH -H "Content-Type:application/json" -d "{\"name\": \"Sales\"}" "http://localhost:8000/api/v1/department/1/"

但是当我尝试在其他资源中做同样的事情时

curl -v -X PATCH -H "Content-Type:application/json" -d "{\"name\": \"XYZ\"}" "http://localhost:8000/api/v1/client/1/"

返回以下错误:

"error_message": "unsupported operand type(s) for ** or pow(): 'Decimal' and NoneType'"
"traceback": "Traceback (most recent call last):
File \"C:\\Python27\\lib\\site-packages\\tastypie\\resources.py\", line 192, in wrapper\n    response = callback(request, *args, **kwargs)
File \"C:\\Python27\\lib\\site-packages\\tastypie\\resources.py\", line 406, in dispatch_detail\n    return self.dispatch('detail', request, **kwargs)
File \"C:\\Python27\\lib\\site-packages\\tastypie\\resources.py\", line 427, in dispatch\n    response = method(request, **kwargs)
File \"C:\\Python27\\lib\\site-packages\\tastypie\\resources.py\", line 1332, in patch_detail\n    self.update_in_place(request, bundle, deserialized)
File \"C:\\Python27\\lib\\site-packages\\tastypie\\resources.py\", line 1346, in update_in_place\n    return self.obj_update(original_bundle, request=request, pk=original_bundle.obj.pk)
File \"C:\\Python27\\lib\\site-packages\\tastypie\\resources.py\", line 1824, in obj_update\n    bundle.obj.save()
File \"C:\\Python27\\lib\\site-packages\\django\\db\\models\\base.py\", line 463, in save\n    self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File \"C:\\Python27\\lib\\site-packages\\django\\db\\models\\base.py\", line 529, in save_base\n    rows = manager.using(using).filter(pk=pk_val)._update(values)
File \"C:\\Python27\\lib\\site-packages\\django\\db\\models\\query.py\", line 557, in _update\n    return query.get_compiler(self.db).execute_sql(None)
File \"C:\\Python27\\lib\\site-packages\\django\\db\\models\\sql\\compiler.py\", line 986, in execute_sql\n    cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File \"C:\\Python27\\lib\\site-packages\\django\\db\\models\\sql\\compiler.py\", line 808, in execute_sql\n    sql, params = self.as_sql()
File \"C:\\Python27\\lib\\site-packages\\django\\db\\models\\sql\\compiler.py\", line 951, in as_sql\n    val = field.get_db_prep_save(val, connection=self.connection)
File \"C:\\Python27\\lib\\site-packages\\django\\db\\models\\fields\\__init__.py\", line 874, in get_db_prep_save\n    self.max_digits, self.decimal_places)
File \"C:\\Python27\\lib\\site-packages\\django\\db\\backends\\__init__.py\", line 809, in value_to_db_decimal\n    return util.format_number(value, max_digits, decimal_places)
File \"C:\\Python27\\lib\\site-packages\\django\\db\\backends\\util.py\", line 149, in format_number\n    return u'%s' % str(value.quantize(decimal.Decimal(\".1\") ** decimal_places, context=context))
TypeError: unsupported operand type(s) for ** or pow(): 'Decimal' and 'NoneType'\n"}* Closing connection #0

curl -v -X PATCH -H "Content-Type:application/json" -d "{\"name\": \"ZZZZ\"}" "http://localhost:8000/api/v1/contact/1/"

返回

The 'department' field has was given data that was not a URI, not a dictionary-alike and does not have a 'pk' attribute: <Bundle for obj: 'Department object' and with data: '{'id': u'1', 'name': u'Sales', 'resource_uri': '/api/v1/department/1/'}'>.* Closing connection #0

我想我的外键定义有问题,但我不知道是什么。当我做GET操作时,一切都很好。我的定义有什么问题?非常感谢你。

0 个答案:

没有答案