Skip to main content
Warning: You are using the test version of PyPI. This is a pre-production deployment of Warehouse. Changes made here affect the production instance of TestPyPI (testpypi.python.org).
Help us improve Python packaging - Donate today!

Implementation of localized model fields using PostgreSQL HStore fields.

Project Description

django-localized-fields is an implementation of a field class for Django models that allows the field’s value to be set in multiple languages. It does this by utilizing the hstore type (PostgreSQL specific), which is available as models.HStoreField in Django 1.10.

Installation

  1. Install the package from PyPi:

    $ pip install django-localized-fields
    
  2. Add localized_fields to your INSTALLED_APPS:

    INSTALLED_APPS = [
        ....
    
        'localized_fields'
    ]
    

Usage

Basic usage

Declare fields on your model as LocalizedField:

from django.db import models
from localized_fields.fields import LocalizedField


class MyModel(models.Model):
    title = LocalizedField()

During migration, the field type will be changed to hstore. From now on you can store multi-language content in this field:

new = MyModel()
new.title.en = 'english title'
new.title.nl = 'dutch title'
new.title.ro = 'romanian title'
new.save()

django-localized-fields integrates with Django’s i18n system, in order for certain languages to be available you have to correctly configure the LANGUAGES and LANGUAGE_CODE settings:

LANGUAGE_CODE = 'en' # default language
LANGUAGEs = (
     ('en', 'English'),
     ('nl', 'Dutch'),
     ('ro', 'Romanian')
)

By changing the active language you can control which language is presented:

from django.utils import translation

translation.activate('nl')
print(new.title) # prints 'dutch title'

translation.activate('en')
print(new.title) # prints 'english title'

Or get it in a specific language:

print(new.title.get('en')) # prints 'english title'
print(new.title.get('ro')) # prints 'romanian title'
print(new.title.get()) # whatever language is the currently active one

You can also explicitly set a value in a certain language:

new.title.set('en', 'other english title')
new.title.set('nl', 'other dutch title')

new.title.ro = 'other romanian title'

Constraints

By default, the following constraints apply to a LocalizedField:

  • Only the default language is required. The other languages are optional and can be NULL.
  • If null=True is specified on the LocalizedField, then none of the languages are required.

At the moment it is not possible to specifically instruct LocalizedField to mark certain languages as required or optional.

Other fields

Besides LocalizedField, there’s also:

  • LocalizedAutoSlugField
    Automatically creates a slug for every language from the specified field. Depends upon:
    • django-autoslug

    Currently only supports populate_from. Example usage:

    from django.db import models
    from localized_fields.fields import (LocalizedField,
                                         LocalizedAutoSlugField)
    
    class MyModel(models.Model):
         title = LocalizedField()
         slug = LocalizedAutoSlugField(populate_from='title')
    
  • LocalizedBleachField
    Automatically bleaches the content of the field.
    • django-bleach

    Example usage:

    from django.db import models
    from localized_fields.fields import (LocalizedField,
                                         LocalizedBleachField)
    
    class MyModel(models.Model):
         title = LocalizedField()
         description = LocalizedBleachField()
    
Release History

Release History

This version
History Node

1.1

History Node

1.0

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
django-localized-fields-1.1.tar.gz (9.4 kB) Copy SHA256 Checksum SHA256 Source Oct 21, 2016

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting