如何在ManytoManyField中设置queryset或limit或filter

时间:2016-06-07 11:39:09

标签: django python-2.7 django-models django-admin

大家好,我是Django的新手,直到现在我使用简单模型和通用视图。

现在我有两个模型,ServerInventaryGroups,其中InventoryGroupsServers的组。

class Server(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    name = models.CharField(max_length=20, null=True)
    hostname = models.CharField(max_length=50, null=True, blank=True)
    ip = models.GenericIPAddressField()
    ip2 = models.GenericIPAddressField(null=True, blank=True)
    user_name = models.CharField(max_length=20, null=True)
    password = models.CharField(max_length=200, null=True, blank=True)
    ssh_key = models.FileField(null=True, blank=True)
    status = models.CharField(max_length=10, null=True, blank=True)
    last_login_time = models.DateTimeField(null=True, blank=True)
    last_login_from = models.GenericIPAddressField(null=True, blank=True)

    def __str__(self):
        return str(self.user) + " - " + self.name

class InventoryGroups(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    name = models.CharField(max_length=20, null=True)
    host = models.ManyToManyField(Server)
    description = models.CharField(max_length=100, null=True, blank=True)

    def get_hosts(self):
        if self.host:
            hostlist = []
            for name in self.host.all():
                hostlist.append(name)
            return hostlist

    def __str__(self):
        return str(self.user) + " - " + self.name

我要做的是根据Server限制我的User(服务器列表)。像这样:

Admin example

您可以在主机列表中查看所有服务器。 但我想只显示由Severs

中选定的经过身份验证的用户或用户添加的InventoryGroups.user int numberOfItems = 100000; int[] array = new int[numberOfItems]; int count = 0; for (int i = numberOfItems / 2; i > 0; i--) { array[count++] = i; } for (int i = 0; i < numberOfItems / 2; i++) { array[count++] = i; }

0 个答案:

没有答案