Code Monkey home page Code Monkey logo

django-locality's Introduction

django-locality

django-locality is a collection of helpers for handling countries and territories in Django. Currently, it includes:

  • locality.json, a fixture with the world's countries and territories (if you notice any missing, open an issue or submit a pull request)
  • views for getting serialized values (e.g. for use in a form)
  • nifty form fields and widgets

Installation

The latest version of django-locality is currently not available on PyPi

Install from GitHub with pip:

pip install -e git+https://github.com/naftulikay/django-locality.git#egg=django-locality==0.2.4

Then add locality to INSTALLED_APPS in your Django settings. To load the included data, run:

python manage.py loaddata locality

(or whichever equivalent method you use to run "manage.py" commands)

The fixture is not installed as initial_data.json to give you the option of using your own, and to prevent changes being overwritten whenever you run syncdb.

Usage

List all countries:

>>> from locality.models import Country
>>> print Country.objects.all()
[<Country: Andorra>, <Country: United Arab Emirates>,
<Country: Antigua and Barbuda>, ...]

or list territories by country:

>>> from locality.models import Country
>>> for country in locality.models.Country.objects.all():
>>>     print country.territories.all()
...
[<Territory: Salta, Argentina>, <Territory: Buenos Aires, Argentina>,
<Territory: Ciudad Autónoma de Buen os Aires, Argentina>, ...]

You can create your own models around countries and territories:

class Address(models.Model):
    country = models.ForeignKey('locality.models.Country')
    territory = models.ForeignKey('locality.models.Territory')

Bugs / TODO

Please report all bugs to the GitHub issue tracker, and enjoy the library :-)

Wishlist:

  • template tags
  • translated country names (see issue #1)

MySQL issues

You will have issues loading fixture data with MySQL if your database's default character set is not utf8. You can check your tables' charsets by running:

SHOW FULL COLUMNS tablename;

You can alter the charsets via:

ALTER TABLE tablename CONVERT TO CHARACTER SET utf8;

To do this for django-locality, you can first sync the database, alter the character set for the resulting tables, and then load the fixtures.

Upgrading from previous versions

django-locality used to create a locality_country table with a too-small name field, and does not (yet) include database migrations. If you were previously using an earlier version and encounter errors when trying to load locality.json, try deleting and recreating the relevant tables:

$ python manage.py dbshell
MariaDB [awesomeproj]> DROP TABLE locality_territory;
Query OK, 0 rows affected (0.01 sec)

MariaDB [awesomeproj]> DROP TABLE locality_country;
Query OK, 0 rows affected (0.01 sec)

MariaDB [awesomeproj]> exit
Bye
$ python manage.py syncdb

and then attempt to load the fixture again.

django-locality's People

Contributors

lacrymology avatar mariocesar avatar naftulikay avatar supervacuo avatar yourcelf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

django-locality's Issues

Too many levels of symbolic links

running pip install git+git://github.com/rfkrocktk/[email protected]#egg=django-locality fails as this:
error: src/django_locality.egg-info/MANIFEST.in: Too many levels of symbolic links. I think you just need to remove the django_locality.egg-info directory from source control

DatabaseError: value too long for type character varying(32) with initial_data.json

Both Country.name and Territory.name have max_length=32, but the following items in initial_data.json have names longer than 32 characters:

Model: Country
ID: 239
Name: South Georgia and SouthSandwich Islands
(39 characters)

Model: Country
ID: 581
Name: United States MinorOutlying Islands
(35 characters)

Model: Territory
ID: 23
Name: Tierra del Fuego, Ant\u00e1rtida e Islas del Atl\u00e1ntico Sur
(63 characters)

Latest version installable from PyPI is 0.1.1

$ pip install -v django-locality==0.2.2
Downloading/unpacking django-locality==0.2.2
  Could not fetch URL http://pypi.python.org/simple/django-locality/0.2.2: HTTP Error 404: Not Found
  Will skip URL http://pypi.python.org/simple/django-locality/0.2.2 when looking for download links for django-locality==0.2.2
  Ignoring link https://pypi.python.org/packages/source/d/django-locality/django-locality-0.1.1.tar.gz#md5=e9aba5177a8ab82bfdec49934b21bd7b (from https://pypi.python.org/simple/django-locality/), version 0.1.1 doesn't match ==0.2.2
  Could not find a version that satisfies the requirement django-locality==0.2.2 (from versions: )
No distributions matching the version for django-locality==0.2.2
Exception information:
Traceback (most recent call last):
  File "/home/carl/.virtualenvs/coolfarmtool/lib/python2.7/site-packages/pip-1.1-py2.7.egg/pip/basecommand.py", line 104, in main
    status = self.run(options, args)
  File "/home/carl/.virtualenvs/coolfarmtool/lib/python2.7/site-packages/pip-1.1-py2.7.egg/pip/commands/install.py", line 245, in run
    requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
  File "/home/carl/.virtualenvs/coolfarmtool/lib/python2.7/site-packages/pip-1.1-py2.7.egg/pip/req.py", line 978, in prepare_files
    url = finder.find_requirement(req_to_install, upgrade=self.upgrade)
  File "/home/carl/.virtualenvs/coolfarmtool/lib/python2.7/site-packages/pip-1.1-py2.7.egg/pip/index.py", line 186, in find_requirement
    raise DistributionNotFound('No distributions matching the version for %s' % req)
DistributionNotFound: No distributions matching the version for django-locality==0.2.2

Storing complete log in /home/carl/.pip/pip.log

and:

$ pip install django-locality
Downloading/unpacking django-locality
  Downloading django-locality-0.1.1.tar.gz
  Running setup.py egg_info for package django-locality
  ...

Translations for country names.

Hello :], this is a idea and not a real issue.

What about allow country name translations?
We can have a table like:

class Country(models.Model):
    ...
    def get_translation(self,language):
        try:
            return self.translations.get(country=self.iso3,language=language).name
        except (ObjectDoesNotExist,AttributeError):
            return self.name
class CountryTranslations(models.Model):
    country = models.ForeignKey(Country,to_field='iso3',related_name='translations')
    language = models.CharField(max_length=15)
    name = models.CharField(max_length=32)

We have a list of possible language codes in locale module (or we can search on other places).

Also, we can put it in gettext files in some way, so we can keep it versioned and without need of fixtures.

What u think about?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.