PostgreSQL has this nifty feature called composite types that you can use to create your own types from the built-in PostgreSQL types. It’s a bit like hstore, only structured, which makes it great for structured data that you might reuse multiple times in a model, like addresses.
Unfortunately to date, they were pretty much a pain to use in Django. There were some older implementations for versions of Django before 1.7, but they tended to do things like create surprise new objects in the namespace, not be migrateable, and require connection to the DB at any time (i.e. during your build).
Anyway, after reading a bunch of their implementations and then the Django source code I wrote django-postgres-composite-types.
pip install django-postgres-composite-types
Then you can define a composite type declaratively:
from django.db import models<br />
from postgres_composite_type import CompositeType</p>
<p>class Address(CompositeType):<br />
<p> address_1 = models.CharField(max_length=255)<br />
address_2 = models.CharField(max_length=255)</p>
<p> suburb = models.CharField(max_length=50)<br />
state = models.CharField(max_length=50)</p>
<p> postcode = models.CharField(max_length=10)<br />
country = models.CharField(max_length=50)</p>
<p> class Meta:<br />
db_type = 'x_address' # Required<br />
And use it in a model:
class Person(models.Model):<br />
<p> address = Address.Field()<br />
The field should provide all of the things you need, including formfield etc and you can even inherit this field to extend it in your own way:
class AddressField(Address.Field):<br />
def __init__(self, in_australia=True, **kwargs):<br />
self.in_australia = in_australia</p>
<p> super().__init__(**kwargs)<br />
Finally to set up the DB there is a migration operation that will create the type that you can add:
import address<br />
from django.db import migrations</p>
<p> operations = [<br />
# Registers the type<br />
field=address.Address.Field(blank=True, null=True),<br />
It’s not smart enough to add it itself (can you do that?). Nor would it be smart enough to write the operations to alter a type. That would be a pretty cool trick. But it’s useful functionality all the same, especially when the alternative is creating lots of 1:1 models that are hard to work with and hard to garbage collect.
It’s still pretty early days, so the APIs are subject to change. PRs accepted of course.