Tastypie无法序列化ForeignKey字段

时间:2014-12-16 16:10:17

标签: python tastypie

我有3个型号:BzMediaMusician,BzMediaSinger和BzMediaAlbum。 BzMediaAlbum有2个字段id_singer和id_musician与2个模型BzMediaMusician,BzMediaSinger相关。

在这里,id_singer和id_musician可以为null,因为某些专辑没有

id_singer = models.ForeignKey('BzMediaSinger', null=True, blank=True, db_column='id_singer')
id_musician = models.ForeignKey('BzMediaMusician', null=True, blank=True, db_column='id_musician')

我已请求httpx:// localhost:8000 / api / album /获取结果

{
"meta": {
"limit": 20,
"next": "/api/album/?limit=20&offset=20",
"offset": 0,
"previous": null,
"total_count": 501
},
"objects": [
{
"album_cat": "69",
"album_name_ascii": "truong ca kinh pho mon",
"album_viewed": 1618,
"album_viewed_month": 0,
"des": "Võ Tá Hân phổ nhạc",
"id_album": 7,
"name_en": "",
"name_vn": "Trường ca kinh Phổ Môn",
"order": "1",
"pic_thumb": "http://local.com/nhac/img/truong_ca_kinh_pho_mon.jpg",
"resource_uri": "/api/album/7/"
},]

上面的结果是它不包含id_singer和id_musician。 我所需要的就是在这两个领域拥有音乐家和歌手的完整信息。 请帮忙。

模型

class BzMediaMusician(models.Model):
    id_musician = models.IntegerField(primary_key=True)
    name_full = models.CharField(max_length=255)
    dignity = models.CharField(max_length=255)
    des_vn = models.TextField()
    des_en = models.TextField()
    content_vn = models.TextField()
    content_en = models.TextField()
    pic_thumb = models.CharField(max_length=255)
    pic_full = models.CharField(max_length=255)
    show_list = models.IntegerField()
    order = models.IntegerField()
    enabled = models.IntegerField()
    nhacsi_type = models.CharField(max_length=1)
    nhacsi_name_ascii = models.CharField(max_length=500)

    class Meta:
        managed = False
        db_table = 'bz_media_musician'


class BzMediaSinger(models.Model):
    id_singer = models.AutoField(primary_key=True)
    name_full = models.CharField(max_length=255)
    dignity = models.CharField(max_length=255)
    des_vn = models.TextField()
    des_en = models.TextField()
    content_vn = models.TextField()
    content_en = models.TextField()
    pic_thumb = models.CharField(max_length=255)
    pic_full = models.CharField(max_length=255)
    show_list = models.IntegerField()
    order = models.IntegerField()
    enabled = models.IntegerField()
    type = models.IntegerField()
    singer_name_ascii = models.CharField(max_length=500)

    class Meta:
        managed = False
        db_table = 'bz_media_singer'


class BzMediaAlbum(models.Model):
    id_album = models.AutoField(primary_key=True)
    name_vn = models.CharField(max_length=255)
    name_en = models.CharField(max_length=255)
    des = models.TextField()
    order = models.BigIntegerField()
    enabled = models.IntegerField()
    id_group = models.BigIntegerField()
    id_parent = models.BigIntegerField()
    id_singer = models.ForeignKey('BzMediaSinger', null=True, blank=True, db_column='id_singer')
    id_musician = models.ForeignKey('BzMediaMusician', null=True, blank=True, db_column='id_musician')
    pic_thumb = models.CharField(max_length=250)
    top_album = models.IntegerField()
    album_name_ascii = models.CharField(max_length=500)
    album_viewed = models.IntegerField()
    album_viewed_month = models.IntegerField()
    album_player = models.IntegerField(null=True, blank=False)
    album_cat = models.CharField(max_length=120)

    class Meta:
        managed = False
        db_table = 'bz_media_album' 

资源

  class BzMediaSingerResource(MyModelResource):
        class Meta:
            filtering = {
                "enabled": ALL,
                "name_full": ALL,
            }
            ordering = ['order']
            queryset = BzMediaSinger.objects.filter(enabled=1)
            resource_name = 'singer'


    class BzMediaMusicianResource(MyModelResource):
        class Meta:
            filtering = {
                "enabled": ALL,
                "name_full": ALL,
            }
            ordering = ['order']
            queryset = BzMediaMusician.objects.filter(enabled=1)
            resource_name = 'musician'


    class BzMediaAlbumResource(MyModelResource):
        class Meta:
            filtering = {
                "enabled": ALL,
                "album_cat": ALL,
                "name_vn": ALL,
                "pic_thumb": ALL,
                "id_group": ALL,
                "id_parent": ALL,
            }
            ordering = ['order']
            excludes = ['enabled', 'album_player', 'top_album', 'id_group', 'id_parent']
            queryset = BzMediaAlbum.objects.filter(enabled=1)
            resource_name = 'album'       

0 个答案:

没有答案