I have been trying to get a ManyToMany working in TastyPie, but th e link just fails to show up. Its a simple relationship, a Family record with multiple address records, but when I display a family record via the api, address just doesn't show, as though the field and relationship doesn't exit.
Can someone tell me what I'm doing wrong
class Family(models.Model):
''' This is the Family Object table .
Contacts link to this table, but this table has a many to many relationship
with the addresses '''
FamilyName = models.CharField(max_length=100, unique=True)
Address = models.ManyToManyField('Address')
Active = models.BooleanField(default=True)
def __unicode__(self):
return self.FamilyName
class Meta:
ordering = ['FamilyName']
class Address(models.Model):
''' This table hold all the addresses for families, contacts and staff.
All contact detail should be here, phones, addresses, postal titles etc
are stored in these records
'''
HomeSalutation = models.CharField(max_length=100)
PostalTitle = models.CharField(max_length=100)
AddressLine1 = models.CharField(max_length=100)
AddressLine2 = models.CharField(max_length=200)
AddressLine3 = models.CharField(max_length=100, blank=True)
AddressLine4 = models.CharField(max_length=100, blank=True)
PostCode = models.CharField(max_length=100)
Country = models.CharField(max_length=100, null=True, blank=True)
Phone1 = models.CharField(max_length=150, blank=True)
Phone2 = models.CharField(max_length=150, null=True, blank=True)
Phone3 = models.CharField(max_length=150, null=True, blank=True)
EmailAddress = models.EmailField(blank=True)
Note = models.TextField(blank=True)
Active = models.BooleanField(default=True)
def __unicode__(self):
return u'%s , %s' % (self.PostalTitle, self.PostCode)
def ListAddress(self):
returnAddress=''
if self.AddressLine1:
returnAddress+=self.AddressLine1
if self.AddressLine2:
returnAddress+=', %s' % (self.AddressLine2)
if self.AddressLine3:
returnAddress+=', %s' % (self.AddressLine3)
if self.AddressLine4:
returnAddress+=', %s' % (self.AddressLine4)
if self.PostCode:
returnAddress+=', %s' % (self.PostCode)
return returnAddress
class AddressResource(ModelResource):
class Meta:
queryset=Address.objects.all()
resource_name='address'
filtering = {
"HomeSalutation":ALL,
"PostalTitle": ALL,
"AddressLine1": ALL,
"AddressLine2": ALL,
"AddressLine3": ALL,
"AddressLine4": ALL,
"PostCode": ALL,
"Phone1" : ALL,
"Phone2": ALL,
"Phone3": ALL,
"Active": ALL,
}
authentication = BasicAuthentication()
allowed_methods = ['get']
cache=SimpleCache()
class FamilyResource(ModelResource):
Address = fields.ToManyField('AddressResource','Address',null=True,full=True)
class Meta:
queryset=Family.objects.filter(Active=True)
resource_name='family'
filtering = {
"id" : ALL,
"FamilyName" : ALL,
}
authentication = BasicAuthentication()
allowed_methods = ['get']
cache=SimpleCache()
TastyPie is picky about exposing foreign keys. I suggest zapping the FamilyResource.Address
reference, then re-add it using code from the tutorial.
http://django-tastypie.readthedocs.org/en/latest/tutorial.html#creating-more-resources