简体   繁体   English

如何在Tastypie中验证独特的约束约束?

[英]How to validate unique together constraint in Tastypie?

I have a model with unique_togeter = True and i'm using a ModelForm to validate it. 我有一个unique_togeter = True的模型,我正在使用ModelForm来验证它。

models.py models.py

class Restrict(BaseModel):
    user = models.ForeignKey(User)
    title = models.ForeignKey('title')
    active = models.BooleanField(default=True)
    created = models.DateTimeField(auto_now_add=True, default=datetime.now())
    updated = models.DateTimeField(auto_now=True, default=datetime.now())

    class Meta:
        ordering = ["id"]
        db_table = "editor_restrict"
        app_label = "geral"
        unique_together = ('user', 'title')

resources.py resources.py

class TitleResource(ModelResource):

    class Meta:
        queryset = Titulo.objects.all()
        always_return_data = True
        fields = ['name']


class UserResource(ModelResource):

    class Meta:
        queryset = User.objects.all()
        fields = ['username', 'email', 'first_name']
        authentication = Authentication()
        authorization = Authorization()
        always_return_data = True
        filtering = {
            'username': ALL,
        }


class RestrictResource(ModelResource):
    user = fields.ForeignKey(UserResource, attribute='user', full=True)
    title = fields.ForeignKey(TitleResource, attribute='title')

    class Meta:
        authentication = Authentication()
        authorization = Authorization()
        queryset = Restrict.objects.all()
        resource_name = 'restrict'
        always_return_data = True
        fields = ['user', 'title', 'active']
        serializer = Serializer()
        validation = FormValidation(form_class=RestrictForm)
        filtering = {
            'user': ALL_WITH_RELATIONS,
        }

That's working nicely when i do a POST like that: 当我做这样的POST时,这很好用:

curl -H 'Content-Type: application/json' -X POST --data '{"active": true, "title": "/api/v1/titulo/16/", "user":"/api/v1/user/52831/"}' 'http://localhost:8000/api/v1/restrict/'

The response is Status Code: 400 BAD REQUEST, as expected. 响应是状态代码:400 BAD REQUEST,如预期的那样。

BUT with the full user like this: 但是像这样的完整用户:

curl -H 'Content-Type: application/json' -X POST --data '{"active": true, "title": "/api/v1/titulo/16/", "user": {"username": "johnny@mail.com", "email": "johnny@mail.com", "first_name": "Johnny"}}' 'http://localhost:8000/api/v1/restrict/'

I get the TypeError Exception int() argument must be a string or a number, not 'dict' from django. 我得到TypeError Exception int()参数必须是字符串或数字,而不是django的'dict'。

The UserResource is working just fine too in all the CRUD. UserResource在所有CRUD中都运行得很好。

If i take off the unique_together from Model this POST works just fine too. 如果我从Model中取下unique_together,这个POST也可以正常工作。

I tryed debug but i can't figure out what's happening. 我尝试调试但我无法弄清楚发生了什么。

The question is: How can i validate this case and return the correct response? 问题是:我如何验证此案例并返回正确的答案?

Thanks. 谢谢。

  1. Possible issue: unique together converts to unique constraint in mysql, and it allows duplicates if at least one of the columns is null. 可能的问题:unique一起转换为mysql中的唯一约束,如果至少有一列为null,则允许重复。
  2. Add title default value, or restrict to not null (if you will alow null it may cause 1.). 添加标题默认值,或限制为非空(如果您将为null,则可能导致1.)。
  3. Try to add some debug info to RestrictResource save, so you will see which dict appears in foregn int field. 尝试向RestrictResource save添加一些调试信息,这样你就可以看到foregn int字段中出现了哪个dict。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM