MajorGeek
MajorGeek

Reputation: 495

IntegrityError user_id may not be NULL

this is my forms.py file

 def save(self,ip_address, *args, **kwargs):
        g = GeoIP()
        lat,lon = g.lat_lon(ip_address)
        user_location = super(registerForm, self).save()
        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)
        if rform.is_valid():
            register = rform.save(ip_address='203.99.178.139')
            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}) 

but when i try to submit the forms it says "IntegrityError :accounts_register.user_id may not be NULL"but i have register.user=request.userin my views. do i have to pass the "Id" or "PK" as an argument to forms or em i making a mistake

Upvotes: 0

Views: 1557

Answers (1)

istruble
istruble

Reputation: 13702

You are probably dealing with an AnonymousUser object and the id will always be set to None. If you are running ./manage.py runserver, try using pdb by putting in a line like this before the statement that is failing:

import pdb; pdb.set_trace()

That will kick you into a REPL where you can step through and explore your code.

Upvotes: 1

Related Questions