Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using ipware in model pre_save #30

Closed
anuj9196 opened this issue Dec 16, 2017 · 1 comment
Closed

using ipware in model pre_save #30

anuj9196 opened this issue Dec 16, 2017 · 1 comment

Comments

@anuj9196
Copy link

I have a model in my project without view which creates a new associated record on post_save of parent model.

I used

from django.urls import reverse
from ipware.ip import get_real_ip

class ShortUrlVisitLogs(models.Model):
     shorturl = models.ForeignKey(ShortUrl, on_delete=models.CASCADE, blank=True)
     ip_address = models.CharField(max_length=225, blank=True, null=True)
     visit_time = models.DateTimeField(auto_now_add=True)

@receiver(pre_save, sender=ShortUrlVisitLogs)
def pre_save_short_url_visit_logs(sender, instance, *args, **kwargs):
    instance.ip_address = get_real_ip(request)

Since this does not require any view. I want to use get_real_ip(request) in the model pre_save receiver as shown above.

But this is returning error

module 'django.http.request' has no attribute 'META'
@un33k
Copy link
Owner

un33k commented Dec 16, 2017

A valid Django request object is get_real_ip()'s requirement. Best is to get the user's IP address in the very first request and store it in user's session or DB. Then you can use it wherever you want.

StackOverflow is the right place for usage & how-to questions.

@un33k un33k closed this as completed Dec 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants