1

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)

Ahmed
  • 280
  • 1
  • 12

1 Answers1

1

You try to specify the model UsersByIP this way usersbyip=request.META.get('REMOTE_ADDR') but usersbyip expects either the id of the model or the model instance itself not a field of the model. You should pass it the ip_user variable of yours (after making sure it is saved):

ip_user, created = UsersByIP.objects.get_or_create(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=ip_user
    )
Abdul Aziz Barkat
  • 19,475
  • 3
  • 20
  • 33
  • Thank you sir this work fine with me, you are life saver – Ahmed Apr 16 '21 at 05:48
  • @Abdul Aziz Barkat - It is storing `ip of Device` or `id of internet provider` or `ip of site` ? – Lars Apr 16 '21 at 07:47
  • @Progam that really depends on the use. If there are proxies in between `REMOTE_ADDR` will give the last proxy and not the user. there is also `HTTP_X_FORWARDED_FOR` which can be used. To be able to get the user's IP address reliably it is best to use some 3rd party package that knows what it is doing, for eg: [django-ipware](https://github.com/un33k/django-ipware) – Abdul Aziz Barkat Apr 16 '21 at 07:54
  • I think the `ip of device` will be **127.0.0.1:8000** in local host. Am i right ? Then how someone can test it in local host ? – Lars Apr 16 '21 at 07:56
  • 2
    @Progam this question ([How to access the local Django webserver from outside world](https://stackoverflow.com/questions/2260727/how-to-access-the-local-django-webserver-from-outside-world)) would be helpful for you if you want to test this – Abdul Aziz Barkat Apr 16 '21 at 07:58