如何配置Tastypie将字段视为唯一?

joh*_*sta 7 python django tastypie

如何配置Tastypie将字段视为唯一?如果我尝试为标记为唯一的字段插入重复条目,我的期望是接收某种非500错误(可能是409冲突?)作为响应.


我查看了文档,看起来它应该对我来说很明显,但由于某种原因,我没有得到我期望看到的响应.

这是文档链接:

http://readthedocs.org/docs/django-tastypie/en/latest/fields.html?highlight=unique


示例代码如下:

urls.py

v1_api = Api(api_name='v1')
v1_api.register(CompanyResource())

urlpatterns = patterns('',
    (r'^api/', include(v1_api.urls)),
)
Run Code Online (Sandbox Code Playgroud)

resource.py

class CompanyResource(ModelResource):

    CompanyName = fields.CharField(attribute='company_name')
    CompanyId = fields.CharField(attribute='company_id', unique=True)
    Contact = fields.CharField(attribute='contact')
    Email = fields.CharField(attribute='email')
    Phone = fields.CharField(attribute='phone')

    class Meta:
        queryset = Company.objects.all()
        authentication = BasicAuthentication()
        authorization = Authorization()
        allowed_methods = ['get', 'post']
Run Code Online (Sandbox Code Playgroud)

models.py

class Company(models.Model):

    company_name = models.TextField(default=None, blank=True, null=True)
    company_id = models.CharField(default='', unique=True, db_index=True, max_length=20)
    contact = models.TextField(default=None, blank=True, null=True)
    email = models.EmailField(default=None, blank=True, null=True)
    phone = models.TextField(default=None, blank=True, null=True)
Run Code Online (Sandbox Code Playgroud)

我收到的错误如下(使用curl来点击我的本地服务):

curl --dump-header - -H "Content-Type: application/json" -X POST --user user:password --data '{"CompanyName": "company", "CompanyId": "1234567890", "Contact": "John", "Email": "example@example.com", "Phone": "555-555-5555"}' http://localhost:8000/api/v1/company/
HTTP/1.0 500 INTERNAL SERVER ERROR
Date: Thu, 15 Sep 2011 18:25:20 GMT
Server: WSGIServer/0.1 Python/2.7.1
Content-Type: application/json; charset=utf-8

{"error_message": "(1062, \"Duplicate entry '1234567890' for key 'api_company_company_id_uniq'\")", 
...<snip>...
raise errorclass, errorvalue\n\nIntegrityError: (1062, \"Duplicate entry '1234567890' for key 'api_company_company_id_uniq'\")\n"}
Run Code Online (Sandbox Code Playgroud)

当我unique=True, db_index=True,从公司模型中删除时,我没有收到完整性错误,而是创建了一个新的重复资源.同样,这不是预期的结果,因为我期望预先形成一些验证并导致一些非500响应.

joh*_*sta 8

以下是我解决问题的方法:

根据验证文档,我能够实现一个自定义验证器,为我检查字段的唯一性. http://django-tastypie.readthedocs.org/en/latest/validation.html

在CompanyResource中,我向类meta添加了CustomValidation.我将CustomValidation的实现放在validations.py文件中.如果isValid返回错误,则api将返回400,其中包含错误消息.

class CompanyResource(ModelResource):
    """
    CompanyIds should be unique
    """     
    CompanyName = fields.CharField(attribute='company_name')     
    CompanyId = fields.CharField(attribute='company_id', unique=True)     
    Contact = fields.CharField(attribute='contact')     
    Email = fields.CharField(attribute='email')     
    Phone = fields.CharField(attribute='phone')    

    class Meta:        
        queryset = Company.objects.all()        
        authentication = BasicAuthentication()        
        authorization = Authorization()        
        allowed_methods = ['get', 'post']                
        validation = CustomValidation()
Run Code Online (Sandbox Code Playgroud)

validations.py

class CustomValidation(Validation):
    """
    The custom validation checks two things:
       1) that there is data
       2) that the CompanyId exists (unique check)
    """
    def is_valid(self, bundle, request=None):
        if not bundle.data:
            return {'__all__': 'Missing data, please include CompanyName, CompanyId, Contact, Email, and Phone.'}

        errors = {}                                    
        company_id=bundle.data.get('CompanyId', None)

        # manager method, returns true if the company exists, false otherwise
        if Company.objects.company_exists(company_id):
            errors['CompanyId']='Duplicate CompanyId, CompanyId %s already exists.' % company_id
        return errors
Run Code Online (Sandbox Code Playgroud)


tho*_*lin 5

我今天遇到了同样的问题.这是我如何处理它:

覆盖资源定义中的[request_method] _ [request_type]方法.例如,我覆盖FooResource中的post_list:

def post_list(self, request, **kwargs):
    from django.db import IntegrityError
    try:
        return super(FooResource, self).post_list(request, **kwargs)
    except IntegrityError, e:
        if e.args[0] == 1062:
            return http.HttpConflict()
Run Code Online (Sandbox Code Playgroud)

希望对你有效.