简体   繁体   English

web2py:不完全相同的字段约束

[英]web2py : non identical field constraint

I would like to define the following table with a non identical field value as a follower should not follow himself : 我想用不相同的字段值定义下表,因为关注者不应该关注他自己:

db.define_table('followers', 
    Field('follower', 
          db.auth_user, 
          requires=(db.subscription.follower != db.subscription.user)),
    Field('user', 
          db.auth_user, 
          requires=(db.subscription.follower != db.subscription.user))
)

But I don't know how to implement it. 但是我不知道如何实现它。 Any hint ? 有什么提示吗?

Thank you 谢谢

Assuming inserts will happen via form processing: 假设将通过表单处理进行插入:

db.define_table('followers', 
    Field('follower', 'reference auth_user',
          requires=IS_IN_DB(db(db.auth_user.id != request.post_vars.user),
                            'auth_user.id', db.auth_user._format)),
    Field('user', 'reference auth_user'))

This will allow any auth_user ID in the user field, but will limit the follower field to IDs other than the one submitted in request.post_vars.user . 这将允许在user字段中使用任何auth_user ID,但会将follower字段限制为request.post_vars.user提交的ID以外的ID。 When there is no form submission, request.post_vars.user will simply be None , which doesn't matter because the validator is used only upon form submission. 如果没有表单提交,则request.post_vars.user将简单地为None ,这无关紧要,因为仅在提交表单时才使用验证器。

Alternatively, you can use an onvalidation callback function when processing the form. 或者,您可以在处理表单时使用onvalidation回调函数。

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

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