Django,djangorest从模特中获取父母

时间:2016-03-10 16:03:02

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

在序列化此模型时,如何获取父类别。

我可以让孩子们

class Categories(models.Model):
    id = models.AutoField(primary_key=True)
    parent_id = models.ForeignKey('Categories', models.DO_NOTHING, blank=True, null=True, db_column='parent_id', related_name='child_categories')
    language_id = models.ForeignKey('Languages', on_delete=models.DO_NOTHING, db_column='language_id', related_name="categories", default='en')
    name = models.CharField(max_length=255, blank=True, null=True)
    slug = models.SlugField(unique=True, max_length=255)
    date_created = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)

    class Meta:
        managed = True
        db_table = 'categories'
        verbose_name = 'category'
        verbose_name_plural = 'categories'
        ordering = ['name','-date_modified', '-date_created',]
        unique_together = (('name', 'language_id'),)

    def __str__(self):              # __unicode__ on Python 2
        return '{0}'.format(self.name)

0 个答案:

没有答案