mirror of
https://github.com/SectorLabs/django-localized-fields.git
synced 2025-04-25 11:42:54 +03:00
Detailed README on setup and usage
This commit is contained in:
parent
666335f3a7
commit
5d20ebbc07
51
README.rst
51
README.rst
@ -19,6 +19,13 @@ django-localized-fields
|
|||||||
|
|
||||||
This package requires Python 3.5 or newer and Django 1.10 or newer.
|
This package requires Python 3.5 or newer and Django 1.10 or newer.
|
||||||
|
|
||||||
|
In the pipeline
|
||||||
|
------------
|
||||||
|
We're working on making this easier to setup and use. Any feedback is apreciated. Here's a short list of things we're working to improve:
|
||||||
|
|
||||||
|
* Make it unnecesarry to add anything to your `INSTALLED_APPS`.
|
||||||
|
* Make it unnecesarry to modify your migrations manually to enable the PostgreSQL HStore extension.
|
||||||
|
|
||||||
Installation
|
Installation
|
||||||
------------
|
------------
|
||||||
1. Install the package from PyPi:
|
1. Install the package from PyPi:
|
||||||
@ -27,20 +34,32 @@ Installation
|
|||||||
|
|
||||||
$ pip install django-localized-fields
|
$ pip install django-localized-fields
|
||||||
|
|
||||||
2. Add ``localized_fields`` to your ``INSTALLED_APPS``:
|
2. Add ``localized_fields`` and ``django.contrib.postgres`` to your ``INSTALLED_APPS``:
|
||||||
|
|
||||||
.. code-block:: bash
|
.. code-block:: bash
|
||||||
|
|
||||||
INSTALLED_APPS = [
|
INSTALLED_APPS = [
|
||||||
....
|
....
|
||||||
|
|
||||||
|
'django.contrib.postgres',
|
||||||
'localized_fields'
|
'localized_fields'
|
||||||
]
|
]
|
||||||
|
|
||||||
|
3. Set ``LANGUAGES` and `LANGUAGE_CODE`` in your settings:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
LANGUAGE_CODE = 'en' # default language
|
||||||
|
LANGUAGEs = (
|
||||||
|
('en', 'English'),
|
||||||
|
('nl', 'Dutch'),
|
||||||
|
('ro', 'Romanian')
|
||||||
|
)
|
||||||
|
|
||||||
Usage
|
Usage
|
||||||
-----
|
-----
|
||||||
|
|
||||||
Basic usage
|
Preparation
|
||||||
^^^^^^^^^^^
|
^^^^^^^^^^^
|
||||||
Declare fields on your model as ``LocalizedField``:
|
Declare fields on your model as ``LocalizedField``:
|
||||||
|
|
||||||
@ -54,15 +73,21 @@ Declare fields on your model as ``LocalizedField``:
|
|||||||
title = LocalizedField()
|
title = LocalizedField()
|
||||||
|
|
||||||
|
|
||||||
During migration, the field type will be changed to ``hstore``. From now on you can store multi-language content in this field:
|
Create your migrations using ``python manage.py makemigrations``. Open the generated migration in your favorite editor and setup the HStore extension before the first ``CreateModel`` or ``AddField`` operation by adding a migration with the `HStoreExtension` operation. For example:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
new = MyModel()
|
from django.contrib.postgres.operations import HStoreExtension
|
||||||
new.title.en = 'english title'
|
|
||||||
new.title.nl = 'dutch title'
|
class Migration(migrations.Migration):
|
||||||
new.title.ro = 'romanian title'
|
...
|
||||||
new.save()
|
|
||||||
|
operations = [
|
||||||
|
HStoreExtension(),
|
||||||
|
...
|
||||||
|
]
|
||||||
|
|
||||||
|
Then apply the migration using ``python manage.py migrate``.
|
||||||
|
|
||||||
``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:
|
``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:
|
||||||
|
|
||||||
@ -75,6 +100,16 @@ During migration, the field type will be changed to ``hstore``. From now on you
|
|||||||
('ro', 'Romanian')
|
('ro', 'Romanian')
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Basic usage
|
||||||
|
^^^^^^^^^^^
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
new = MyModel()
|
||||||
|
new.title.en = 'english title'
|
||||||
|
new.title.nl = 'dutch title'
|
||||||
|
new.title.ro = 'romanian title'
|
||||||
|
new.save()
|
||||||
|
|
||||||
By changing the active language you can control which language is presented:
|
By changing the active language you can control which language is presented:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
Loading…
x
Reference in New Issue
Block a user