Django REST框架-带有附加参数的HyperlinkedRelatedField [英] Django REST framework - HyperlinkedRelatedField with additional parameter

查看:39
本文介绍了Django REST框架-带有附加参数的HyperlinkedRelatedField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django Rest框架构建REST Web API.我在类别和钱包之间存在多对多关系.从类别开始,我检索与其链接的所有钱包,然后从外壳上开始一切正常

I'm building REST Web API with Django Rest framework. I have a many-to-many relation between categories and wallets. Starting from a category I retrieve all wallets its linked to, and from the shell everything works fine

型号:

class Wallet(models.Model):
    nome = models.CharField(max_length=255)
    creato = models.DateTimeField(auto_now=False)
    utente = models.ForeignKey(User)
    wallettype = models.ForeignKey(Wallettype)
    payway = models.ManyToManyField(Payway, through = 'Wpw', blank = True)
    defaultpayway = models.ForeignKey('Wpw', related_name = 'def_pw', null = True)

    def __unicode__(self):
        return self.nome

class Category(models.Model):
    nome = models.CharField(max_length=255)
    creato = models.DateTimeField(auto_now=False)
    enus = models.ForeignKey(Enus)
    wallet = models.ManyToManyField(Wallet)
    utente = models.ForeignKey(User)
    owner = models.ManyToManyField(User, related_name='owner_cat')

网址:

urlpatterns = patterns(
    '',
    url(r'^$', views.cat_list.as_view()),
    url(r'^/(?P<pk>[0-9]+)/*$', views.cat_detail.as_view()),
    url(r'^/(?P<cat_id>[0-9]+)/wallets/*$', views.cwallet_list.as_view()),
    url(r'^/(?P<cat_id>[0-9]+)/wallets/(?P<pk>[0-9]+)/*$', views.cwallet_detail.as_view(), name='cwallet-detail'),
)

序列化器:

class cat_serializer(serializers.ModelSerializer):
    wallet = serializers.HyperlinkedRelatedField(
        many=True,
        read_only=True,
        view_name='cwallet-detail',
        # lookup_field = 'pk',
        # lookup_url_kwarg = 'pk'
    )
    subcat_count = serializers.IntegerField(
        source='subcategory_set.count',
        read_only=True
    )

    class Meta:
        model = Category
        fields = ('id', 'nome','wallet','subcat_count')

当我打电话时:

GET http://localhost:8000/categories/77/wallets

我想找回

{
    'nome': 'Dinner',
    'subcat_count': 12,
    'wallet': {
        'http://localhost:8000/77/wallet/1',
        'http://localhost:8000/77/wallet/2',
        'http://localhost:8000/77/wallet/3',
    }
}

但是它不起作用,并且出现此错误:
无法使用视图名称"cwallet-detail"解析超链接关系的URL.您可能无法在API中包含相关模型,或者在此字段上错误地配置了 lookup_field 属性."

But it doesn't work and I get this error:
"Could not resolve URL for hyperlinked relationship using view name "cwallet-detail". You may have failed to include the related model in your API, or incorrectly configured the lookup_field attribute on this field."

我认为问题与其他参数有关:实际上,在我的urls.py中,我的钱包ID为pk,类别ID为cat_id.我不知道如何将类别ID传递给'cwallet-detail'.

I think that the problem is linked to the additional param: in fact in my urls.py I have pk for the wallet id and cat_id for the category id. I can't figure how to pass the category id to the 'cwallet-detail'.

有人知道如何将第二个参数传递给HyperlinkedRelatedField吗?
预先谢谢你.

Anyone know how to pass a second paramater to the HyperlinkedRelatedField?
Thank you in advance.

推荐答案

阅读文档并发布新问题" rel ="nofollow">汤姆·克里斯蒂(Tom Christie)的Django REST github ,现在我知道它不受官方支持,但是重复和补丁正在开发中.

After reading docs and posting a new issue on Tom Christie's Django REST github, now I know that it's not officially supported, but doumentations and patch are work in progress.

实际上有 此链接 用于构建自定义超链接字段来解决该问题.

Actually there is this link for building a Custom Hyperlinked Filed to solve the problem.

希望这对遇到相同问题的人有所帮助.

Hope this help someone who has same problem.

这篇关于Django REST框架-带有附加参数的HyperlinkedRelatedField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆