1

我有一个简单的多对多关系概述如下:

class Client(models.Model):
    """
    just stores a Client hostname now
    """
    hostname = models.CharField(max_length=250, null=False, blank=False, unique=True)

    def __unicode__(self):
        """
        """
        return str(self.hostname)

class Run(models.Model):
    clients = models.ManyToManyField(Client)

使用 django-rest-framework 我现在有过滤器可以识别特定的客户端:

"/api/runs/?clients=1" 工作并检索所有与主键为 1 的客户端的运行。

如何启用允许 "/api/runs/?clients=myhostname" 的过滤器?

4

2 回答 2

2

The answer is below.

In short you must overwrite the get_queryset method.

http://www.django-rest-framework.org/api-guide/filtering/#filtering-against-query-parameters

于 2013-08-01T16:46:57.443 回答
1

尝试这个:

filter(clients__hostname=clients)

看看https://docs.djangoproject.com/en/1.5/topics/db/queries/#spanning-multi-valued-relationships

于 2013-08-01T13:19:53.453 回答