I have a method:
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
Object of the Host model are ipv4_sub_net and ipv6_sub_net. My motive is for defining the above method is to get the value from ipv4_sub_net + ipv6_sub_net model field which will be fetched from Host model corresponding to a requesting user (loginned user). To do this i passed reuest.user.username to the get_netmask parameter at the time of calling it. Apart from that i also want to return the count os similar entry in ipv4_sub_net and ipv6_sub_net seperately.
For example:
In table there are four coloum present:
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
So, let’s say that the requesting user is with user_id 2. So according to method definitions it will return the dictionary. First index of dictionary contain unique IPv4_sub_net + ipv6_sub_net which is "1.0.0.1" and /23 and the second index will return the count of returning similar sub_net for ipv4 and the third index will return the count of similarsub_netfor ipv6 which2 for ipv4and2 for ipv6`.
Try changing
to
Edit
On the first pass, I missed another error. You should also change
to
getwill always return a single object and throw an error if multiple objects are returned. If you’re expecting a single object, then I’m not really sure what you’re trying to do.