Django:获取子类别各自的类别或逆序

时间:2016-03-05 07:36:30

标签: python django python-2.7 django-models django-rest-framework

我是django的初学者。我希望将所有子类别分别列入相应类别或可逆顺序。

class Category(models.Model):
    name_en = models.CharField(max_length=100)
    name_pt = models.CharField(max_length=100)
    parent = models.ForeignKey('self', null = True, blank = True, related_name = "subcategory")
    status = models.CharField(max_length=1, blank=True, null=True, default='1', help_text = "1= Active, 0= Inactive")

class Meta:
    db_table = 'news_category'

def __unicode__(self):
    return self.id        

这是serializer.py中的序列化程序类:

class CategorySerializer(serializers.ModelSerializer):

    class Meta:
        model = Category

我想要序列化的数据如下:

{
  "status": 200,
  "data": [
   {
  "child": [],
  "id": 1,
  "name_en": "India",
  "name_pt": "Índia",
  "parent": null,
  "status": "1"
},
{
  "child": [  {
       "child":[],
       "id": 6,
       "name_en": "XYZ",
       "name_pt": "XYZ",
       "parent": 2,
       "status": "1"
      },
      {
      "child": [],
       "id": 7,
       "name_en": "ABC",
       "name_pt": "ABC",
       "parent": 2,
       "status": "1"
      }
   ],
  "id": 2,
  "name_en": "World",
  "name_pt": "mundo",
  "parent": null,
  "status": "1"
},
{
  "child": [],
  "id": 3,
  "name_en": "Cali",
  "name_pt": "cali",
  "parent": 3,
  "status": "1"
}
],
"error": 0
}

以上Json child拥有父数据中的所有数据。我想要这种类型的json请帮帮我。

https://gist.github.com/mhulse/1031882

以上链接提供了父母的详细信息。但我也需要孩子的详细信息或逆序。

0 个答案:

没有答案