Opened 17 years ago

Closed 12 years ago

#5446 closed New feature (wontfix)

New model fields for languages and countries at least

Reported by: Raphaël Hertzog <raphael@…> Owned by: Marinho Brandão
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: feature_request
Cc: chris@…, semente@…, gregor@…, hr.bjarni+django@…, hector+django@…, mmitar@…, flavio.curella@…, jeffrey@…, windbottle, gezuru@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

I think that many applications have to deal either with language specific content and/or countries. It would be nice if Django provided ready to use fields for languages and countries instead of asking every user to handle the list and its full translation by himself (it could be a contrib package though). IMO this is best done by creating some glue on one of the available open-source reference lists. At Debian, we use a package called iso-codes that is very well-translated and kept up-to-date as it's used by our installer.

http://svn.debian.org/wsvn/pkg-isocodes/trunk/iso-codes/?rev=0&sc=0

I can also mention the django-countries project created by Chris Moffitt (who created Satchmo, a Django-powered e-commerce framework). Its extent is somewhat more limited but it's already developed specifically for Django.

http://code.google.com/p/django-countries/

And then, of course, there's Babel:
http://babel.edgewall.org

The idea is to have something like:

country = models.CountryField()
language = models.LanguageField()

Instead of:

COUNTRIES = (
    ('fr', _('France')),
    ('de', _('Germany')),
    ...
)
country = models.CharField(max_length=10, choices=COUNTRIES)

which requires redoing the translation work and which is tedious to manually maintain.

Attachments (10)

country_and_language_fields.diff (11.7 KB ) - added by Marinho Brandão 17 years ago.
fixed
country_and_language_fields_maxlength.diff (12.8 KB ) - added by Martin Conte Mac Donell <Reflejo@…> 17 years ago.
Just changed maxlength (deprecated) to max_length
country_and_language_fields_maxlength_2.diff (11.7 KB ) - added by Martin Conte Mac Donell <Reflejo@…> 17 years ago.
Just changed maxlength (deprecated) to max_length (Sorry about the last patch, it was merged with other one, please delete it.)
country_codes.py (9.7 KB ) - added by lcordier 17 years ago.
Example program to maintain & sort counties.
country_and_language_fields_trunk.patch (11.7 KB ) - added by Martín Conte Mac Donell <Reflejo@…> 17 years ago.
Patch against trunk
country_and_language_fields_trunk.2.patch (11.8 KB ) - added by Martín Conte Mac Donell <Reflejo@…> 17 years ago.
This ticket is cursed. There is the patch against trunk
country_and_language_fields_trunk.3.patch (12.1 KB ) - added by lcordier 16 years ago.
Make it patch against the latest trunk, some things moved around, and validator_list seems to be gone.
country_and_language_fields_trunk.4.patch (12.4 KB ) - added by lcordier 15 years ago.
Patch with form-presentation improvements, countries are now sorted.
country_and_language_fields_trunk.20100409.patch (22.2 KB ) - added by Hector Lecuanda 15 years ago.
Apply against latest trunk, moved field definitions to localflavor so as to keep contrib as 'rm -rf -able', added working tests
geo_fields.py (13.0 KB ) - added by Mitar 14 years ago.
Geo fields with initial values based on GeoIP

Download all attachments as: .zip

Change History (43)

comment:1 by Malcolm Tredinnick, 17 years ago

Generally a good idea. The only requirement is that the list has to be something we can ship in Django. The standard Django installation should have essentially no external dependencies, apart from the db wrapper.

Definitely agree about the idea of creating a list for this.

comment:2 by chris@…, 17 years ago

I think it would be possible to include the Satchmo models and intial_data.xml as something like django.contrib.iso_codes

All of the info is BSD licensed through Satchmo so I have no issue including in Django.

You can see the models here-
http://www.satchmoproject.com/trac/browser/satchmo/trunk/satchmo/l10n/models.py

It uses the initial_data from here-
http://www.satchmoproject.com/trac/browser/satchmo/trunk/satchmo/l10n/fixtures

Let me know if you'd be interested in using this.

Also, just for the record, I did not create django-countries ;)

comment:3 by Philippe Raoult, 17 years ago

Keywords: feature_request added

comment:4 by Michael Radziej, 17 years ago

Component: Database wrapperContrib apps
Triage Stage: UnreviewedAccepted

The positive reaction of Malcolm looks like it should be Accepted (though the solution isn't clear).

Raphaël or Chris: Can you please bring this up on the developer list? It would be nice to introduce this to more people and see what they think.

comment:6 by zeke, 17 years ago

Django-countries is based in another project, WebDa. And looks that Chris Moffitt was not accomplishing with the license of its author. And it's very possible than django-countries is being a derivated work of WebDa (but it isn't beind credited), because the definition of AREAS is exactly equal than the one original.

I'll try to contact with its original author.

comment:7 by chris.moffitt@…, 17 years ago

Originally satchmo was using Webda. There were some licensing concerns about the data but not the models. The current satchmo implementation is fully BSD licensed. As I mentioned on the developer thread, I'm willing to help someone get this into Django but I don't have the bandwidth to spin this off into another project.

comment:8 by Marinho Brandão, 17 years ago

I wrote the following snippets who does exactly what you want.

http://www.djangosnippets.org/snippets/493/
http://www.djangosnippets.org/snippets/494/

Can I wrote these as patch fix for this task?

comment:9 by Marinho Brandão, 17 years ago

Owner: changed from nobody to Marinho Brandão
Status: newassigned

comment:10 by Marinho Brandão, 17 years ago

Component: Contrib appsDatabase wrapper
Has patch: set
Resolution: fixed
Status: assignedclosed

Fixings. I did it at db wrapper componente because was I understood of following Malcolm's sentence:

"The standard Django installation should have essentially no external dependencies, apart from the db wrapper."

comment:11 by Jacob, 17 years ago

Resolution: fixed
Status: closedreopened

comment:12 by Jacob, 17 years ago

I really don't like sticking all the countries in global settings; that's the wrong place for it. I think that the right place would be in django.contrib.localflavor.generic. Otherwise, though, a very good patch.

by Marinho Brandão, 17 years ago

fixed

comment:13 by Marinho Brandão, 17 years ago

Patch updated with validators and COUNTRIES settings moved to localflavor.generic package.

by Martin Conte Mac Donell <Reflejo@…>, 17 years ago

Just changed maxlength (deprecated) to max_length

by Martin Conte Mac Donell <Reflejo@…>, 17 years ago

Just changed maxlength (deprecated) to max_length (Sorry about the last patch, it was merged with other one, please delete it.)

comment:14 by lcordier, 17 years ago

The source of the country_codes, http://xml.coverpages.org/country3166.html, is a bit outdated,

  • Yugoslavia and Zaire should be removed
  • Ivory Coast should be renamed to Côte d'Ivoire (some ascii codec have problems with the ô) so use Cote d'Ivoire instead.
  • Add ('CD', _('Democratic Republic of the Congo')),.

I would also suggest that the country list be sorted alphabetically by the country name as to appear
more sensible in the select box (widget).

http://www.iso.org/iso/iso3166_en_code_lists.txt, http://en.wikipedia.org/wiki/ISO_3166-1

by lcordier, 17 years ago

Attachment: country_codes.py added

Example program to maintain & sort counties.

comment:15 by Guilherme M. Gondim <semente@…>, 17 years ago

Cc: semente@… added

by Martín Conte Mac Donell <Reflejo@…>, 17 years ago

Patch against trunk

by Martín Conte Mac Donell <Reflejo@…>, 17 years ago

This ticket is cursed. There is the patch against trunk

by lcordier, 16 years ago

Make it patch against the latest trunk, some things moved around, and validator_list seems to be gone.

comment:16 by lcordier, 16 years ago

You'll have to create django/tests/modeltests/countries/__init__.py (empty file) since it appears patch -p0 < patchfile does not create it.

by lcordier, 15 years ago

Patch with form-presentation improvements, countries are now sorted.

comment:17 by Gonzalo Saavedra, 15 years ago

Cc: Gonzalo Saavedra added

Is this ticket ready for check in? Patch looks good.

comment:18 by Alex Gaynor, 15 years ago

No, it's not. If for no other reason than this ticket violates the, "contrib should be rm -rf able".

comment:19 by idris, 15 years ago

I suggest adding a slug to the COUNTRIES list, as it would be nice to be able to use pretty url's based on a CountryField, like /afghanistan/ or /brazil/

comment:20 by Gregor Müllegger, 15 years ago

Cc: gregor@… added

I like the COUNTRIES choices in the django/contrib/localflavor/generic/init.py file of the patch. It would be nice if there would be something like prefered countries that appear first in the choicelist - because Afghanistan is a quite bad default in most cases. Here is a possible implementation that uses a PREFERED_COUNTRIES setting:

def prefer_countries(countries, prefer=(), separator='-' * 5):
    prefered_countries = []
    for country in countries:
        if country[0] in prefer:
            prefered_countries.append(country)
    countries = list(countries)
    countries = prefered_countries + [('', separator)] + countries
    return tuple(countries)

COUNTRIES = prefer_countries(COUNTRIES, settings.PREFERED_COUNTRIES)

What do you think?

comment:21 by hejsan, 15 years ago

Cc: hr.bjarni+django@… added

comment:22 by anonymous, 15 years ago

Cc: hector+django@… added

by Hector Lecuanda, 15 years ago

Apply against latest trunk, moved field definitions to localflavor so as to keep contrib as 'rm -rf -able', added working tests

comment:23 by Mitar, 14 years ago

Cc: mmitar@… added

comment:24 by Mitar, 14 years ago

Why LanguageField is not fixed with choices:

kwargs.setdefault('choices', settings.LANGUAGES)

And isValid functions are probably unnecessary as form validation is not done anymore with validator_list.

comment:25 by Mitar, 14 years ago

I am attaching a new version of geo fields. I over all data and I corrected it according to Wikipedia values (some values were missing or were invalid or not best possible). I also extended them also to a city field for which I added necessary data with major (not necessary capital in all cases) city for every country.

I also extended initial values to be a callable which takes a request as a parameter so that it is possible to do GeoIP based initial values. I explained this and how it is possible to make this work in #13972. If request is not specified it falls back to values from settings so it is also backwards compatible. (Only loading of GeoIP could probably also be made optional.)

by Mitar, 14 years ago

Attachment: geo_fields.py added

Geo fields with initial values based on GeoIP

comment:26 by Flavio Curella, 14 years ago

Cc: flavio.curella@… added

comment:27 by Gabriel Hurley, 14 years ago

Severity: Normal
Type: New feature

comment:28 by patchhammer, 14 years ago

Easy pickings: unset
Patch needs improvement: set

country_and_language_fields_trunk.20100409.patch fails to apply cleanly on to trunk

comment:29 by Jeffrey Gelens, 13 years ago

Cc: jeffrey@… added
UI/UX: unset

comment:30 by windbottle, 13 years ago

Cc: windbottle added

comment:31 by gezuru@…, 13 years ago

Cc: gezuru@… added

comment:32 by Gonzalo Saavedra, 13 years ago

Cc: Gonzalo Saavedra removed

comment:33 by Claude Paroz, 12 years ago

Resolution: wontfix
Status: reopenedclosed

Now that contrib.localflavor is gone, I don't see why we would include this. django-countries seems to be a well-maintained external app.

Note: See TracTickets for help on using tickets.
Back to Top