Django-rest-framework多个URL参数 [英] Django-rest-framework multiple url arguments

查看:113
本文介绍了Django-rest-framework多个URL参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何将示例对象"映射到网址: website.com/api/<user>/< slug> .

How do i map the "example object" to the url: website.com/api/<user>/<slug>.

我得到这个
int()以10为底的无效文字:用户名" 错误.所以我知道我需要使用用户ID才能映射到该对象,这是因为如果我使用user_id(整数)(URL: website.com/api,则可以映射到该对象/< user_id>/< slug> ),而不仅仅是用户/用户名(字符串).

I'm getting this
invalid literal for int() with base 10: 'username' error. so i understand that i need to use the user id in-order to map to the object, this is because I am able to map to the object if i use the user_id (integer) (url: website.com/api/<user_id>/<slug>) instead of just the user/username (string).

在从user_id(整数)到另一个字段(例如user(字符串))映射到对象时,是否有方法可以覆盖默认值?

Is there a way to override the default when mapping to the object from user_id (integer) to another field like user (string)?

我也不明白为什么在(Api View)的 def get_object 中传递用户而不是user_id不能解决此问题.

Also i don't understand why passing the user instead of user_id in the def get_object in (Api View) does not fix this problem.

网址

urlpatterns = [
    url(r'^api/(?P<user>\w+)/(?P<slug>[\w-]+)/$', ExampleDetailAPIView.as_view(), name='example'),
]

Api视图

class ExampleDetailAPIView(RetrieveAPIView):
    queryset = Example.objects.all()
    serializer_class = ExampleDetailSerializer

    def get_object(self):
        user = self.kwargs.get('user')
        slug = self.kwargs.get('slug')
        return Example.objects.get(user=user, slug=slug)

    def get_serilizer_context(self, *args, **kwargs):
        return {'request': self.request}

序列化器

class ExampleDetailSerializer(HyperlinkedModelSerializer):
    url = serializers.SerializerMethodField()

    class Meta:
        model = Example
        fields = [
            'url',
        ]

    def get_url(self, obj):
        request = self.context.get('request')
        return obj.get_api_url(request=request)

模型

class Example(models.Model):
    user                = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
    example_name         = models.CharField(max_length=100)
    slug                = models.SlugField(max_length=100, blank=True)

    class Meta:
        unique_together = ('user', 'slug')

    def get_api_url(self, request=None):
        return api_reverse('example-api:example', kwargs={'user': self.user.username, 'slug': self.slug}, request=request)

@receiver(pre_save, sender=Example)
def pre_save_example_slug_receiver(sender, instance, *args, **kwargs):
    slug = slugify(instance.example_name)
    instance.slug = slug

推荐答案

您可以在URL中使用用户名.为此,您必须首先手动找到用户,然后使用其 id 查找 Example 对象:

You can use the username in the url. For that to work you'll have to first find the user manually and then use its id to find the Example object:

def get_object(self):
    username = self.kwargs.get('username')
    slug = self.kwargs.get('slug')

    # find the user
    user = User.objects.get(username=username)

    return Example.objects.get(user=user.id, slug=slug)

这篇关于Django-rest-framework多个URL参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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