From models.py:
class emailusername(models.Model):
"""
Store each username just once along with an integer ID which is
automatically created by the ORM.
"""
username = models.CharField(max_length=64);
class emaildomain(models.Model):
"""
Store each domain just once along with an integer ID which is
automatically created by the ORM.
"""
domain = models.CharField(max_length=64);
class emailaddress(models.Model):
"""
Store each email address as a combination of integer@integer
foreign key relationships to the emailusername and emaildomain
classes. Django automatically links the integer ID columns for us.
This is much more space efficient and faster for lookup than
storing each individual email address as a string.
"""
username = models.ForeignKey('emailusername')
domain = models.ForeignKey('emaildomain')
from parsemail.py which imports models from models.py:
(userto,domainto) = parseaddr(mail['To'])[1].split('@')
newemailaddress = emailaddress()
newemailusername = emailusername()
newemaildomain = emaildomain()
newemailusername.username = userto
newemaildomain.domain = domainto
newemaildomain.save()
newemailusername.save()
newemailaddress.username = newemailusername
newemaildomain.domain = newemaildomain
newemailaddress.save()