数据库约束检查中带验证和可选postgresql的django的iban字段
django-iban-field的Python项目详细描述
带有验证器和可选postgresql的Django iban字段,用于基本约束检查这是 预计将与django 1.8及更高版本合作。
安装
安装django-iban-field,就像安装任何其他pypi包一样:
pip install django-iban-field
配置和使用
将django_iban添加到settings.py中的INSTALLED_APPS列表中
在models.py中使用:
from django_iban.fields import IBANField ... class MyModel(models.Model): iban = IBANField(enforce_database_constraint=True, unique=True)
enable_databse_constraint选项将向数据库中添加名为is-valid的函数 使用它强制执行数据库类型检查。此选项只有在您使用 postgreql后端,并在数据库上启用plpython扩展
实用程序
除了django字段之外,如果要生成有效的 IBAN值:
from django_iban.generator import IBANGenerator .... generator = IBANGenerator() # A complete random IBAN valid_iban = generator.generate() # But you can specify the country valid_iban = generator.generate(country_code='DE') # Or the bank and account valid_iban = generator.generate(country_code='DE', bank=XXXX, account=XXXX)
当然,你不能在没有国家的情况下通过银行和账户,如果你通过了一个 或者这两个值都将检查它们的有效性,计算校验和并返回 有效的IBAN。
您还可能对包含所有格式规范的IBANSpecification类感兴趣 以及校验和功能:
from django_iban.specification import IBANSpecification from django_iban.specification import IBAN_SPECIFICATION_CONFIG .... country_specification = IBAN_SPECIFICATION_CONFIG['TN'] print(country_specification.bank_field_length) # 5 print(country_specification.bank_regex) #[0-9]{2}[0-9]{3} print(country_specification.iban_regex) #[A-Z]{2}[0-9]{2}[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}
灵感
本模块从django-localflavor中获得灵感和一些想法 ibanfield和来自tom’s cafe
的规范示例
您可以在这个demo django application中找到这个字段的运行示例,如果您非常懒 您只需获得我的vagrant bootstraper脚本,该脚本将使用该项目设置一个完全工作的vm你 只需要安装vagrant和openssl(在fedora上测试过,但应该可以与任何其他现代linux一起使用 或mac):
curl https://raw.githubusercontent.com/Chedi/DjangoVagrantBootsraper/master/bootstrap.sh | bash