Code

Opened 7 years ago

Closed 16 months ago

#5446 closed New feature (wontfix)

New model fields for languages and countries at least

Reported by: Raphaël Hertzog <raphael@…> Owned by: marinho
Component: Database layer (models, ORM) Version: master
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 6 years ago.
fixed
country_and_language_fields_maxlength.diff (12.8 KB) - added by Martin Conte Mac Donell <Reflejo@…> 6 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@…> 6 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 6 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@…> 6 years ago.
Patch against trunk
country_and_language_fields_trunk.2.patch (11.8 KB) - added by Martín Conte Mac Donell <Reflejo@…> 6 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 6 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 5 years ago.
Patch with form-presentation improvements, countries are now sorted.
country_and_language_fields_trunk.20100409.patch (22.2 KB) - added by hlecuanda 4 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 4 years ago.
Geo fields with initial values based on GeoIP

Download all attachments as: .zip

Change History (43)

comment:1 Changed 7 years ago by mtredinnick

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 Changed 7 years ago by chris@…

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 Changed 7 years ago by PhiR

  • Keywords feature_request added

comment:4 Changed 7 years ago by mir

  • Component changed from Database wrapper to Contrib apps
  • Triage Stage changed from Unreviewed to Accepted

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 Changed 6 years ago by zeke

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 Changed 6 years ago by chris.moffitt@…

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 Changed 6 years ago by marinho

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 Changed 6 years ago by marinho

  • Owner changed from nobody to marinho
  • Status changed from new to assigned

comment:10 Changed 6 years ago by marinho

  • Component changed from Contrib apps to Database wrapper
  • Has patch set
  • Resolution set to fixed
  • Status changed from assigned to closed

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 Changed 6 years ago by jacob

  • Resolution fixed deleted
  • Status changed from closed to reopened

comment:12 Changed 6 years ago by jacob

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.

Changed 6 years ago by marinho

fixed

comment:13 Changed 6 years ago by marinho

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

Changed 6 years ago by Martin Conte Mac Donell <Reflejo@…>

Just changed maxlength (deprecated) to max_length

Changed 6 years ago by Martin Conte Mac Donell <Reflejo@…>

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

comment:14 Changed 6 years ago by lcordier

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

Changed 6 years ago by lcordier

Example program to maintain & sort counties.

comment:15 Changed 6 years ago by Guilherme M. Gondim <semente@…>

  • Cc semente@… added

Changed 6 years ago by Martín Conte Mac Donell <Reflejo@…>

Patch against trunk

Changed 6 years ago by Martín Conte Mac Donell <Reflejo@…>

This ticket is cursed. There is the patch against trunk

Changed 6 years ago by lcordier

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

comment:16 Changed 6 years ago by lcordier

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

Changed 5 years ago by lcordier

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

comment:17 Changed 5 years ago by gonz

  • Cc gonz added

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

comment:18 Changed 5 years ago by Alex

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

comment:19 Changed 4 years ago by idris

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 Changed 4 years ago by phxx

  • 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 Changed 4 years ago by hejsan

  • Cc hr.bjarni+django@… added

comment:22 Changed 4 years ago by anonymous

  • Cc hector+django@… added

Changed 4 years ago by hlecuanda

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

comment:23 Changed 4 years ago by mitar

  • Cc mmitar@… added

comment:24 Changed 4 years ago by mitar

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 Changed 4 years ago by mitar

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

Changed 4 years ago by mitar

Geo fields with initial values based on GeoIP

comment:26 Changed 4 years ago by gogna

  • Cc flavio.curella@… added

comment:27 Changed 3 years ago by gabrielhurley

  • Severity set to Normal
  • Type set to New feature

comment:28 Changed 3 years ago by patchhammer

  • Easy pickings unset
  • Patch needs improvement set

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

comment:29 Changed 3 years ago by jgelens

  • Cc jeffrey@… added
  • UI/UX unset

comment:30 Changed 3 years ago by windbottle

  • Cc windbottle added

comment:31 Changed 2 years ago by gezuru@…

  • Cc gezuru@… added

comment:32 Changed 2 years ago by gonz

  • Cc gonz removed

comment:33 Changed 16 months ago by claudep

  • Resolution set to wontfix
  • Status changed from reopened to closed

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.