我有一个方法:
def get_netmask(user=None):
user = User.objects.get(username=user)
id_user = user.id
obj = Host.objects.get(user=id_user)
obj = obj.ipv4_sub_net.distinct('ipv4_sub_net').order_by('ipv4_sub_net')
# In this line i am getting an error: `'unicode' object has no
# attribute 'distinct'` but the query is not complete . For full
# question please follow the lines and given example
return obj
Host 模型的对象是ipv4_sub_net
和ipv6_sub_net
。我定义上述方法的动机是从 ipv4_sub_net
+ipv6_sub_net
模型字段中获取值,该字段将从Host model
对应于请求用户(登录用户)中获取。为此,我在调用它时传递reuest.user.username
给 get_netmask
参数。ipv4_sub_net
除此之外,我还想ipv6_sub_net
单独返回 count os 类似的条目。
例如:在表中有四个列存在:
id user_id ipv4_sub_net ipv6_sub_net
1 2 1.0.0.1 /23
2 2 8.9.7.3 /23
3 1 23.2.3.4 /43
4 2 1.0.0.1 /23
所以,假设请求用户是user_id
2
. 因此,根据方法定义,它将返回字典。字典的第一个索引包含唯一IPv4_sub_net + ipv6_sub_net
的,"1.0.0.1"
并且第二个索引将返回为 ipv4的sub_net 2为ipv6/23
的返回相似的计数。sub_net
ipv4 and the third index will return the count of similar
for ipv6 which
and