geocoding an IP and save using model forms

后端 未结 2 1403

i have this thing in my models.py file

        user = models.ForeignKey(\'auth.User\', unique = True) 
        latitude = models.DecimalField(max_digits=8,          


        
相关标签:
2条回答
  • 2021-01-16 02:41

    Overwrite the save method of form to populate latitude & longitude

    from django.contrib.gis.utils import GeoIP
    class registerForm(forms.ModelForm): 
        class Meta:
            model=register
            fields = ('Availability', 'Status')
    
        def save(self, ip_address, *args, **kwargs):
            g = GeoIP()
            lat, lon = `get the lat & lng`
            user_location = super(registerForm, self).save(commit=False)
            user_location.latitude = lat
            user_location.longitude = lon
            user_location.save(*args, **kwargs)
    
    0 讨论(0)
  • 2021-01-16 02:49

    change this line

    register = rform.save(commit=False)
    

    to

    register = rform.save(ip_address=request.META['REMOTE_ADDR'])
    
    0 讨论(0)
提交回复
热议问题