我有一个unique_togeter = True的模型,我正在使用ModelForm来验证它。
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')
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,
}
当我像这样执行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/'
响应是状态代码:400 BAD REQUEST,如预期的那样。
但是像这样的完整用户:
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/'
我得到TypeError Exception int()参数必须是字符串或数字,而不是来自django的'dict'。
UserResource在所有CRUD中都运行得很好。
如果我从Model中取消unique_together,这个POST也可以正常工作。
我尝试了调试,但我无法弄清楚发生了什么。
问题是:我如何验证此案例并返回正确的答案?
感谢。
答案 0 :(得分:0)