TastyPie ManyToMany不工作

时间:2014-06-27 13:32:47

标签: django python-2.7 tastypie

我一直在努力让ManyToMany在TastyPie工作,但链接却没有显示出来。它是一个简单的关系,一个包含多个地址记录的Family记录,但是当我通过api显示一个族记录时,地址就不显示了,好像字段和关系没有退出。

有人可以告诉我我做错了吗

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()

1 个答案:

答案 0 :(得分:0)

TastyPie对暴露外键很挑剔。我建议切换FamilyResource.Address引用,然后使用教程中的代码重新添加它。

http://django-tastypie.readthedocs.org/en/latest/tutorial.html#creating-more-resources