Opened 8 years ago

Closed 8 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


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 = ''
idn_domain_good = 'xn--1lq90ic7fzpc.xn--fiqs8s'.decode('idna')   # a IDN domain in UTF format
idn_domain_bad = idn_domain + '#'

# ok!
# ok!
# raises ValidationError ok!
# selecting from db yields:
# ok!

class Zone(models.Model):
    name = models.DomainNameField(accept_idna=False)

# 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)

models_domainnamefield.txt (1.6 KB) - added by michele 8 years ago.
models_domainnamefield_2.txt (1.6 KB) - added by michele 8 years ago.
ensure python string is always UTF

Download all attachments as: .zip

Change History (5)

Changed 8 years ago by michele

Attachment: models_domainnamefield.txt added

comment:1 Changed 8 years ago by michele

Two notes:

  1. the test mentioned above has idn_domain_good = 'xn--1lq90ic7fzpc.xn--fiqs8s'.encode('idna') instead of decode
  2. please review and comment about to_python() being called on both raw and python data (see comment in patch)

Changed 8 years ago by michele

ensure python string is always UTF

comment:2 Changed 8 years ago by michele

Please refer to attachment:models_domainnamefield_2.txt , obsoletes the previous patch.

comment:3 Changed 8 years ago by Claude Paroz

Resolution: wontfix
Status: newclosed

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.

Last edited 8 years ago by Claude Paroz (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top