开发者

converting float to decimal (IP address parameter)

im trying to locate the location of a user using GeoIP for this i have

Class register(models.Model): 

        user = models.ForeignKey('auth.User', unique = True) 
        latitude = models.DecimalField(max_digits=12, decimal_places=10)
        longitude = models.DecimalField(max_digits=12, decimal_places=10)
        Availability  = models.CharField(max_length=8,choices=STATUS_CHOICES, blank= False, null=False)
        Status = models.CharField(max_length=50, blank = True, null= True)

and in forms.py i have

class registerForm(forms.ModelForm): 
        class Meta:
            mode开发者_开发技巧l=register
            fields = ('Availability', 'Status')#'latitude','longitude',

        def save(self,ip_address, *args, **kwargs):
            g = GeoIP()
            lat,lon = g.lat_lon(ip_address)
            user_location = super(registerForm, self).save(commit=False)
            user_location.latitude = lat
            user_location.longitude = lon
            user_location.save(*args, **kwargs)

and in my views i have

def status_set(request):
    if request.method == "POST":
        rform = registerForm(data = request.POST)
    print "rejister form request"
        if rform.is_valid():
            register = rform.save(ip_address='119.153.117.32')
            register.user=request.user
                register.save(ip_address)
                    return render_to_response('home.html')
    else:
        rform = registerForm() 
    return render_to_response('status_set.html',{'rform':rform}) 

i em taking the IP address as "119.153.117.32" because i am accessing the site from local loop and GeoIP do not have a lat,lon for local loop (127.0.0.1). and it returns none for local loop.\ when i submitt the form it says "Cannot convert float to Decimal. First convert the float to a string". how can i solve this issue is it compulsory to convert it to a string or is there any better way of doing it.


Try using models.FloatField for your coordinates instead of DecimalField, or converting your floats to a string using str(lat) etc before saving the object. I'm not sure if DecimalField is appropriate for data that's coming back as floats so I'd prefer to change to FloatField, but that means resyncing your database.

DecimalField is probably intended for stuff that's a python 'decimal' - see here:

http://docs.python.org/library/decimal.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜