3

外部キー制約に関連するリソースに問題があります (** または 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 操作を行うと、すべて問題ありません。私の定義のどこが間違っていますか? どうもありがとうございました。

4

0 に答える 0