Canada (ca)

Forms

Canada-specific Form helpers.

class localflavor.ca.forms.CAPostalCodeField(*, max_length=None, min_length=None, strip=True, empty_value='', **kwargs)[source]

Canadian postal code form field.

Validates against known invalid characters: D, F, I, O, Q, U Additionally the first character cannot be Z or W. For more info see: http://www.canadapost.ca/tools/pg/manual/PGaddress-e.asp#1402170

clean(value)[source]

Validate the given value and return its “cleaned” value as an appropriate Python object. Raise ValidationError for any errors.

class localflavor.ca.forms.CAProvinceField(*, max_length=None, min_length=None, strip=True, empty_value='', **kwargs)[source]

A form field that validates its input is a Canadian province name or abbreviation.

It normalizes the input to the standard two-leter postal service abbreviation for the given province.

clean(value)[source]

Validate the given value and return its “cleaned” value as an appropriate Python object. Raise ValidationError for any errors.

class localflavor.ca.forms.CAProvinceSelect(attrs=None)[source]

A Select widget that uses a list of Canadian provinces and territories as its choices.

class localflavor.ca.forms.CASocialInsuranceNumberField(*, max_length=None, min_length=None, strip=True, empty_value='', **kwargs)[source]

A Canadian Social Insurance Number (SIN).

Checks the following rules to determine whether the number is valid:

clean(value)[source]

Validate the given value and return its “cleaned” value as an appropriate Python object. Raise ValidationError for any errors.

Models

class localflavor.ca.models.CAPostalCodeField(*args, **kwargs)[source]

A model field that stores the Canadian Postal code in the database.

Forms represent it as a CAPostalCodeField field.

New in version 4.0.

formfield(**kwargs)[source]

Return a django.forms.Field instance for this field.

class localflavor.ca.models.CAProvinceField(*args, **kwargs)[source]

A model field that stores the two-letter Canadian province abbreviation in the database.

Forms represent it as a forms.CAProvinceField field.

New in version 4.0.

deconstruct()[source]

Return enough information to recreate the field as a 4-tuple:

  • The name of the field on the model, if contribute_to_class() has been run.
  • The import path of the field, including the class:e.g. django.db.models.IntegerField This should be the most portable version, so less specific may be better.
  • A list of positional arguments.
  • A dict of keyword arguments.

Note that the positional or keyword arguments must contain values of the following types (including inner values of collection types):

  • None, bool, str, int, float, complex, set, frozenset, list, tuple, dict
  • UUID
  • datetime.datetime (naive), datetime.date
  • top-level classes, top-level functions - will be referenced by their full import path
  • Storage instances - these have their own deconstruct() method

This is because the values here must be serialized into a text format (possibly new Python code, possibly JSON) and these are the only types with encoding handlers defined.

There’s no need to return the exact way the field was instantiated this time, just ensure that the resulting field is the same - prefer keyword arguments over positional ones, and omit parameters with their default values.

class localflavor.ca.models.CASocialInsuranceNumberField(*args, **kwargs)[source]

A model field that stores a Canadian Social Insurance Number (SIN) in the format XXX-XXX-XXX.

Forms represent it as forms.CASocialInsuranceNumberField field.

New in version 4.0.

formfield(**kwargs)[source]

Return a django.forms.Field instance for this field.

Data

localflavor.ca.ca_provinces.PROVINCE_CHOICES = (('AB', 'Alberta'), ('BC', 'British Columbia'), ('MB', 'Manitoba'), ('NB', 'New Brunswick'), ('NL', 'Newfoundland and Labrador'), ('NT', 'Northwest Territories'), ('NS', 'Nova Scotia'), ('NU', 'Nunavut'), ('ON', 'Ontario'), ('PE', 'Prince Edward Island'), ('QC', 'Quebec'), ('SK', 'Saskatchewan'), ('YT', 'Yukon'))

An alphabetical list of provinces and territories for use as choices in a formfield. Source: http://www.canada.gc.ca/othergov/prov_e.html

localflavor.ca.ca_provinces.PROVINCES_NORMALIZED = {'ab': 'AB', 'alberta': 'AB', 'b.c.': 'BC', 'bc': 'BC', 'british columbia': 'BC', 'manitoba': 'MB', 'mb': 'MB', 'nb': 'NB', 'new brunswick': 'NB', 'newfoundland': 'NL', 'newfoundland and labrador': 'NL', 'nf': 'NL', 'nl': 'NL', 'northwest territories': 'NT', 'nova scotia': 'NS', 'ns': 'NS', 'nt': 'NT', 'nu': 'NU', 'nunavut': 'NU', 'on': 'ON', 'ontario': 'ON', 'p.e.i.': 'PE', 'pe': 'PE', 'pei': 'PE', 'pq': 'QC', 'prince edward island': 'PE', 'qc': 'QC', 'quebec': 'QC', 'saskatchewan': 'SK', 'sk': 'SK', 'yk': 'YT', 'yt': 'YT', 'yukon': 'YT', 'yukon territory': 'YT'}

a mapping of province misspellings/abbreviations to normalized abbreviations