Django and architecture : how to share a "referencial" database between projects?
Question: I come today with a design/architecture question concerning Django.

I work on several websites (hosted on the same server) which individually need
geographical data (states, towns, etc.). Each project contains apps, and each
app may contain models with `ForeignKey` fields to Town or State.

In order to not repeat myself, I wan't to **build a database to store these
towns and states, and to use it through Django projects**.

Django provides a straightforwards way to use several databases in the same
project, declaring it in the `settings.py` file and writing routers classes to
hold reading and writing stuff. But that way, impossible to use
`select_related` statement like :

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

This behaviour is just natural to me (impossible to make joins between
databases, from scratch)...

My questions :

  * Is it a good idea to consider introducing **dblinks** (I don't think so...) and can Django handle it (I didn't find any docs for this part) ?
  * How would you proceed, facing this situation ? 

A quick and dirty solution would be to import all geo data (towns, states...)
in each project database, but it's not DRY at all :( :

    
    
    python manage.py loaddata geo.json
    

Another solution may be to build a separated "geo" app, which could "serve" (I
don't know how) the data to other projects... Actually, I tried _GeoDjango_ ,
but it seems to be really complex, and it probably won't answer my question !

Thank you very much in advance for your answers !


Answer: Depending upon how static this data is, the simplest way might be to just
define these towns and states in Python once and then import this definition
in all of your separate projects:

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

And then you can, instead of using a Foreign key use a charfield specifying
the options 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)
    

This approach is not very easy to update, and it does not record the
relationship between towns and states (i.e. A town is in one state), however
it is dead simple.

