0
我試圖鏈接添加到具有特定的「類別」 ID「帖子」列表對象沒有屬性 - HyperlinkedRelatedField
,我想建立的網址是/分類/
models.py
class Categories(models.Model):
....
idcategories = models.AutoField(db_column='idCategories', primary_key=True)
class Postings(models.Model):
idpostings = models.AutoField(db_column='idPostings', primary_key=True) # Field name made lowercase.
idcategories = models.ForeignKey(Categories, db_column='idCategories') # Field name made lowercase.
....
serializers.py
class CategorySerializer(serializers.HyperlinkedModelSerializer):
postings = serializers.HyperlinkedRelatedField(many=True, view_name='postings-detail', read_only=True)
class Meta:
model = models.Categories
fields = ('url', 'idcategories', 'categoriesname', 'categoryimageurl', 'postings')
views.py
url(r'postings/(?P<pk>[0-9]+)/$', postings_detail, name='postings-detail'),
我已經建立了相應的意見,這工作,如果我刪除的CategorySerializer
的HyperlinkedRelatedField基本上我想:
JSON:
categoryname : <name>,
postings : [<list_of_postings>]
因此,與上面的代碼,我得到以下錯誤:
AttributeError at/categories/ 'Categories'對象沒有任何屬性'postings'
我基於我的代碼DRF的例子[鏈接](http://www.django-rest-framework.org/tutorial/5-relationships-and-hyperlinked-apis/),它似乎不像他們使用related_name – kaminsky59
http://www.django-rest-framework.org/tutorial/4-authentication-and-permissions/#adding-information-to-our-model <它在那裏 – Linovia