Django and architecture: how to share a "reference" database between projects?

I came today with a design / architecture question regarding Django.

I work on several websites (hosted on the same server) that individually need geographic data (states, cities, etc.). Each project contains applications, and each application can contain models with fields ForeignKeyfor a city or state.

In order not to repeat myself, I cannot create a database to store these cities and states and use it through Django projects .

Django provides an easy way to use multiple databases in one project by declaring it in a file settings.pyand writing classes of routers to store data for reading and writing. But in this way it is impossible to use an operator select_related, for example:

job = get_object_or_404(Jobs.objects.select_related('state__town'), user=user)

This behavior is just natural for me (it is impossible to create connections between databases, from scratch) ...

My questions:

  • It is a good idea to consider introducing dblinks (I don’t think so ...) and can Django handle it (I have not found any documents for this part)?
  • What will you do when faced with such a situation?

A quick and dirty solution is to import all geodata (cities, states ...) into each project database, but not DRY at all :(:

python manage.py loaddata geo.json

"geo", "" ( , ) ... , GeoDjango, , , , !

!

+5
2

, , Python , :

# locations.py
STATES = (('S1', 'State 1'), ('S2', 'State 2'))
TOWNS = (('T1', 'Town 1'), ('T2', 'Town 2'))

charfield, kwarg:

# app/models.py
from django.db import models
import locations # its on the path somewhere!

class MyModel(models.Model):
    state = models.CharField(max_length=5, options=STATES)
    town = models.CharField(max_length=5, options=TOWNS)

, (.. ), .

+1

. . .

+1

All Articles