I created a view to get the Anonymous users IP, I want when this user what the video then register this ip has watched the video,I know it's now efficient because might the user use diffrent network,
This my model of the user by ip:
class UsersByIP(models.Model):
ip_user = models.GenericIPAddressField()
def __str__(self):
return self.ip_user
This model to make relationship between the vdeo and the number of viewrs by ip
class Video(models.Model):
viewers_by_ip = models.ManyToManyField(UsersByIP, default='192.168.0.1', blank=True)
I created this view to register the ip as view but I got this error:
Field 'id' expected a number but got '127.0.0.1'
and I couldn't solv it.
The view:
num_views_by_ip = Video.objects.annotate(
num_views_ip=Count('viewers_by_ip'),
).order_by('-viewers_by_ip')
data = get_object_or_404(num_views_by_ip, pk=pk)
ip_user = UsersByIP(ip_user=request.META.get('REMOTE_ADDR'))
if not request.user.is_authenticated:
__, created = Video.viewers_by_ip.through.objects.get_or_create(
video=data,
usersbyip=request.META.get('REMOTE_ADDR')
)
if created:
data.num_views_by += 1
I would like to get any suggestions to solve this view or make a new view (logic)