I'm using django-salesforce to get a list of Contacts (child) and the Accounts (parent) associated with them.
In my models.py, what is the syntax for defining a field in the Contact class to hold the Account name?
Here's my models.py:
from django.db import models
from salesforce.models import SalesforceModel
class Account(SalesforceModel):
Name = models.CharField(max_length=255)
def __unicode__(self):
return u'%s %s' % (self.Name, self.Id)
class Contact(SalesforceModel):
FirstName = models.CharField(max_length=255)
LastName = models.CharField(max_length=255)
AccountId = models.CharField(max_length=255)
def __unicode__(self):
return u'%s %s %s' % (self.FirstName, self.LastName, self.AccountId)
I've not been able to find any examples, but I was thinking it would look something like this:
AccountName = models.ForeignKey(Account, db_column='Name')
The account name shouldn't be held in a field in
Contact
, since it's already held in Account (this is the purpose of database normalization- to reduce redundancy).Instead you would connect it using a single
ForeignKey
:and get the account name from a
Contact
objectc
with: