私には方法があります:
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
ホストモデルのオブジェクトはと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
。
例:表には、次の4つの列があります。
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
。したがって、メソッド定義に従って、ディクショナリを返します。ディクショナリの最初のインデックスにはuniqueIPv4_sub_net + ipv6_sub_net
が含ま"1.0.0.1"
れています。2番目のインデックスは、 sub_net 2 for ipv4 2foripv6`に対して/23
同様の結果を返すカウントを返します。sub_net
ipv4 and the third index will return the count of similar
for ipv6 which
and