4

免责声明:我正在学习 django,因为我将它应用于我继承的数据库/PHP 应用程序。数据库有点乱,没有外键约束,命名不一致。我不想在数据库上触碰或重做任何事情,因为我根本不想弄乱遗留应用程序。

堆栈:Python 2.7。Django 1.5,Django 休息框架

问题是我有一个关系,其中有一个包含多个代码的想法。股票代码表有想法的外键(teaser_id),所以我们有类似的东西

**Tickers**
id teaser_id 
1  1
2  1
3  1
4  2
4  2 

**Ideas**
id
1
2

我让 django 从数据库中生成模型,但没有 FK 约束它不能正确生成所有关系。所以这里是模型的配置:

class Tickers(models.Model):
    id = models.IntegerField(primary_key=True)

    # I changed to this
    teaser_id = models.ForeignKey(Idea)
    # From        
    # teaser_id = models.IntegerField(null=True, blank=True)

    ticker = models.CharField(max_length=135L, blank=True)
    date_added = models.CharField(max_length=135L, blank=True)
    class Meta:
        db_table = 'teaser_tickers'


class Idea(models.Model):
    id = models.IntegerField(primary_key=True)
    industry_id = models.IntegerField()
    post_type = models.CharField(max_length=45L)

    class Meta:
        db_table = 'idea'

这是我的序列化程序

class TickerSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = myModels.Tickers
        fields = (
            'id'
            ,'teaser_id'
            ,'ticker'
        )

class IdeaSerializer(serializers.HyperlinkedModelSerializer):
    user = UserSerializer(many=False, read_only=True)
    ticker = TickerSerializer(many=True, read_only=True, )
    teaser = myFields.TeaserField(teaser_length=200, original_field='content')

    class Meta:
        model = myModels.Idea
        fields = (
            'id'
            , 'title'
            , 'date_added'
            , 'user'
            , 'teaser'
            , 'ticker'
        )

我希望想法资源将代码作为子节点集返回。

REST 请求适用于代码为子元素的 Idea。所以我得到了一个例外,即在想法中没有定义ticker。很好 - 但我只是在猜测如何在这一点上进行设置 - 我正在浏览文档和源 - 但希望有人能帮助我。

谢谢你

4

2 回答 2

2

As akaphenom said you have to use the related_name in your serializer, but since you don't specify any in your models you must use the default, in this case teaser_set and your IdeaSerializer must be:

class IdeaSerializer(serializers.HyperlinkedModelSerializer):
    user = UserSerializer(many=False, read_only=True)
    tickers = ReverseTickerSerializer(many=True, read_only=True)
    teaser_set = myFields.TeaserField(teaser_length=200, original_field='content')


class Meta:
    model = myModels.Idea
    fields = (
        'id',
        'title',
        'date_added',
        'user',
        'teaser_set',
        'tickers',
    )
于 2013-10-11T23:17:39.477 回答
1

SO the solution for the reverse lookup is specifying the model correctly, and namely the related_name which is the field that is created in the foreign model to perform the reverse look up. Now I specified a custom serializer to limit to the content I am interested in - but that piece is optional.

class Tickers(models.Model):
    id = models.IntegerField(primary_key=True)

    # I changed to this
    teaser_id = models.ForeignKey(Idea, related_name='tickers')
    # From        
    # teaser_id = models.IntegerField(null=True, blank=True)

    ticker = models.CharField(max_length=135L, blank=True)
    date_added = models.CharField(max_length=135L, blank=True)
    class Meta:
        db_table = 'teaser_tickers'


class Idea(models.Model):
    id = models.IntegerField(primary_key=True)
    industry_id = models.IntegerField()
    post_type = models.CharField(max_length=45L)

    class Meta:
        db_table = 'idea'

Here are my serializers

class ReverseTickerSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = myModels.Tickers
        fields = (
            'id'
            ,'ticker'
        )

class IdeaSerializer(serializers.HyperlinkedModelSerializer):
    user = UserSerializer(many=False, read_only=True)
    tickers = ReverseTickerSerializer(many=True, read_only=True)
    teaser = myFields.TeaserField(teaser_length=200, original_field='content')

    class Meta:
        model = myModels.Idea
        fields = (
            'id'
            , 'title'
            , 'date_added'
            , 'user'
            , 'teaser'
            , 'tickers'
        )
于 2013-10-11T22:05:45.030 回答