Go – Django Model Design

database-designdjango

I've previously asked questions for this project, regarding the Django Admin, Inlines, Generics, etc. Thanks again to the people who contributed answers to those. For background, those questions are here:

Django – Designing Model Relationships – Admin interface and Inline

Django Generic Relations with Django Admin

However, I think I should probably review my model again, to make sure it's actually "correct". So the focus here is on database design, I guess.

We have a Django app with several objects – Users (who have a User Profile), Hospitals, Departments, Institutions (i.e. Education Institutions) – all of whom have multiple addresses (or no addresses). It's quite likely many of these will have multiple addresses.

It's also possible that multiple object may have the same address, but this is rare, and I'm happy to have duplication for those instances where it occurs.

Currently, the model is:

class Address(models.Model):
    street_address = models.CharField(max_length=50)
    suburb = models.CharField(max_length=20)
    state = models.CharField(max_length=3, choices=AUSTRALIAN_STATES_CHOICES)
    ...
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey()
    ...
class Hospital(models.Model):
    name = models.CharField(max_length=20)
    address = generic.GenericRelation(Address)
    ...
class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    address = generic.GenericRelation(Address)
    ...

Firstly – am I doing it right, with the FK field on each object that has address(es)?

Secondly, is there a better alternative to using Generic Relations for this case, where different objects all have addresses? In the other post, somebody mentioned using abstract classes – I thought of that, but there seems to be quite a bit of duplication there, since the addresses model is basically identical for all.

Also, we'll be heavily using the Django admin, so it has to work with that, preferrably also with address as inlines.(That's where I was hitting an issue – because I was using generic relationships, the Django admin was expecting something in the content_type and object_id fields, and was erroring out when those were empty, instead of giving some kind of lookup).

Cheers,
Victor

Best Answer

I think what you've done is more complicated than using some kind of subclassing (the base class can either be abstract or not).

class Addressable(models.Model):
  ...   # common attributes to all addressable objects

class Hospital(Addressable):
  name = models.CharField(max_length=20)
  ...

class UserProfile(models.Model):
  user = models.ForeignKey(User, unique=True)

class Address(models.Model):
  street_address = models.CharField(max_length=50)
  suburb = models.CharField(max_length=20)
  state = models.CharField(max_length=3, choices=AUSTRALIAN_STATES_CHOICES)
  ...
  owner = models.ForeignKey(Addressable)

You should consider making the base class (Addressable) abstract, if you don't want a seperate table for it in your database.

Related Topic