2013-02-02 63 views
0

當我做這樣的事情http://example.com/api/v1/nisit/?format=json。我會得到這個錯誤「模型」有一個空的屬性「頁面」,不允許空值。「如何使用tastypie反向關係

我想使用tastypie的反向關係。與「頁面」模型形成「nisit」模型相反。我想要的結果是調用127.0.0.1:8000/api/v1/nisit/?format=json & friend_ followingPage _id = 1

問題的關鍵是「如何設置followingPage在Nisit屬性模型是在頁面模型反向關係到followingNisit屬性

這是我的模型

class Nisit(models.Model): 
     friends = models.ManyToManyField('self',null=True,blank=True) 


class Page(models.Model): 
     followingNisit = models.ManyToManyField(Nisit,blank=True) 

這是我的資源

class NisitResource(ModelResource): 
    friend = fields.ManyToManyField('self','friend',null=True) 
    followingPage = fields.ToManyField('chula.api.resourse.PageResource','followingPage') 
    class Meta: 
     queryset = Nisit.objects.all() 
     resource_name = 'nisit' 
     filtering = { 
      'friend' : ALL_WITH_RELATIONS, 
      'id' : ALL, 
     } 

在上面的代碼中。我嘗試編寫>>>>頁= .................根據這個鏈接http://django-tastypie.readthedocs.org/en/latest/resources.html#reverse-relationships 但它不能幫助

class PageResource(ModelResource): 
    followingNisit = fields.ManyToManyField(NisitResource, 'followingNisit',null=True) 

    class Meta: 
     queryset = Page.objects.all() 
     resource_name = 'page' 
     authorization= Authorization() 
     filtering = { 
      'followingNisit': ALL_WITH_RELATIONS, 
      'p_name': ALL, 
     } 

回答

0

嘗試增加:

class Page(models.Model): 
    followingNisit = models.ManyToManyField(Nisit,blank=True, related_name="followingPage") 

清除您的緩存,也許刪除所有您的null=True可能會抑制該錯誤消息。