Opened 13 years ago
Closed 13 years ago
#18120 closed Uncategorized (wontfix)
add DomainNameField model Field type to validate and store Internet Domain Names
Reported by: | michele | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.4 |
Severity: | Normal | Keywords: | domain |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
In django models you can store an URL, but not a domain name. This patch adds a DomainNameField class to address that.
DomainNameField accepts:
- traditional domain names in ASCII form
- Internationalized Domain Names in IDNA form (ASCII encoded)
- Internationalized Domain Names in UTF form
DomainNameField always stores values in ASCII format by encoding and decoding UTF IDNs as needed.
Implementation notes:
- based on CharField and DomainNameValidator (see #18119)
- an implicit max_length=255 is added to CharField to propagate the Domain constraint down to the db schema
- naming: accept_idna might be renamed to "accept_idn" or simply "idn" for Internationalized Domain Name
This passes successfully a buch of tests:
class Zone(models.Model): name = models.DomainNameField() plain_domain = 'foo.com' idn_domain_good = 'xn--1lq90ic7fzpc.xn--fiqs8s'.decode('idna') # a IDN domain in UTF format idn_domain_bad = idn_domain + '#' Zone(name=plain_domain).full_clean() # ok! Zone(name=idn_domain_good).full_clean() # ok! Zone(name=idn_domain_bad).full_clean() # raises ValidationError ok! Zone(name=idn_domain).save() # selecting from db yields: xn--1lq90ic7fzpc.xn--fiqs8s # ok! class Zone(models.Model): name = models.DomainNameField(accept_idna=False) Zone(name=idn_domain_good).full_clean() # raises ValidationError ok!
I'll implement these in a suitable test module if django.db will start one.
Work for this and DomainNameValidator was sponsored by BuddyNS.
Attachments (2)
Change History (5)
by , 13 years ago
Attachment: | models_domainnamefield.txt added |
---|
comment:1 by , 13 years ago
by , 13 years ago
Attachment: | models_domainnamefield_2.txt added |
---|
ensure python string is always UTF
comment:2 by , 13 years ago
Please refer to attachment:models_domainnamefield_2.txt , obsoletes the previous patch.
comment:3 by , 13 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
I think the DomainNameValidator (#18119) is a useful addition to Django. However, a specific DomainName field seems far too specialized to be maintained into Django. It is probably much more adequate for an external package.
Two notes:
idn_domain_good = 'xn--1lq90ic7fzpc.xn--fiqs8s'.encode('idna')
instead of decode