I'm trying to write application for my customer in Django 1.6.4
I've huge experience with Django but this case is quite hard for me to solve.
I'm using Substituting a custom user model feature and my Member
class looks like (this is only simple snipplet):
class Member(AbstractBaseUser):
...
date_of_birth = models.DateField()
height = models.FloatField()
...
REQUIRED_FIELDS = ['date_of_birth', 'height']
My Member
can define multiple addresses with different countries. Each country has own specific address validations or extra fields, so I defined main abstract address and some subaddresses:
class COUNTRIES(object):
POLAND = 'pl'
GERMANY = 'de'
@classmethod
def to_choices(cls):
return ((cls.POLAND, 'Poland'), (cls.GERMANY, 'Germany'))
class BaseAddress(models.Model):
class Meta:
abstract = True
member = models.ForeignKet(settings.AUTH_USER_MODEL, related_name='+')
country = models.CharField(max_length=2, choices=COUNTRIES.to_choices)
class AddressPL(BaseAddress):
street = models.CharField(max_length=80)
city = models.CharField(max_length=20)
postal_code = models.CharField(max_length=6, validators=[RegexValidator('^\d{2}-\d{3}$'),])
def save(self, *args, **kwargs):
self.country = COUNTRIES.POLAND
super(AddressPL, self).save(*args, **kwargs)
class AddressDE(BaseAddress):
street = models.CharField(max_length=80)
city = models.CharField(max_length=20)
postal_code = models.CharField(max_length=6, validators=[RegexValidator('^\d{5}$'),])
def save(self, *args, **kwargs):
self.country = COUNTRIES.GERMANY
super(AddressPL, self).save(*args, **kwargs)
As You can see the only change is in postal_code
field in validators
section. The only problem is that in Member
I need to add something like this:
class Member(AbstractBaseUser):
...
@property
def addresses(self):
from itertools import chain
addresses_pl = AddressPL.objects.filter(member=self)
addresses_de = AddressDE.objects.filter(member=self)
return list(chain(addresses_pl, addresses_de))
based on 431628-how to combine 2 or more querysets
And my question is. Do You have any other idea to design this? I need to provide easy, scalable solution. Maybe any idea for using proxies?