Django models's attributes correctly saved by forms, but returns None type when queried
<p>I have a model called UserProfile defined like so:</p>

<pre><code>class UserProfile(models.Model):

    user = models.OneToOneField(User)

    website = models.URLField(blank=True, null=True)
    location = models.CharField(max_length=200, null=True)
    longitude = models.FloatField(null=True)
    latitude = models.FloatField(null=True)
    credit = models.FloatField(default=0, null=True)


    def __unicode__(self):
        return self.user.username
</code></pre>

<p>The 'register' page includes the following form:</p>

<p>When it is submitted it goes to:</p>

<pre><code>def register(request):
    registered = False
    if request.method == "POST":
        user_form = UserForm(request.POST)
        profile_form = UserProfileForm(request.POST)

        if user_form.is_valid() and profile_form.is_valid():
            print(request.POST)
            lat = request.POST.get('lat')
            print("lat is: " + lat)
            lng = request.POST.get('lng')
            user = user_form.save()                
            user.set_password(user.password)
            user.save()
            profile = profile_form.save(commit=False)
            profile.user = user
            profile.latitude = lat
            profile.longitude = lng
            profile.save()
        else:
            print user_form.errors, profile_form.errors

    else:
        profile_form = UserProfileForm()
        user_form = UserForm()

    return render(request, "register.html", {'user_form' : user_form, 'profile_form' : profile_form})
</code></pre>

<p>That all seems to work fine, but when I try to query this data in another view, the location, lat, and lng properties come up as 'None' types.</p>

<pre><code>def link(request):
    print("trying to authenticate...")
    if request.user.is_authenticated():
        user_profile = UserProfile(user=request.user)
    else:
        return render(request, 'login.html')

    if request.method == "POST":
        print("request was a post.")
        if request.is_ajax():
            print("request is ajax.")
            print("User profile is: " + str(user_profile))
            # returns billy, so we're looking at the right object
            print("user location is: " + str(user_profile.location)) 
            user_longitude = user_profile.longitude
            user_latitude = user_profile.latitude
            distance = request.POST.get('distance')            
            value = request.POST.get('value')
</code></pre>

<p>As you can see, the data coming up here is different than what is returned by the shell queries:</p>

<pre><code>User profile is billy
user location is: None
user longitude is: None
</code></pre>

<p>I'm very confused as to how location, etc are float types in the shell and register view, but None types in the 'link' view.  Any ideas?</p>

<p>In your <code>link</code> view, you're not querying for the userprofile, you're just instantiating a new (blank) one with the user set to the current user.</p>

<p>You probably meant to do:</p>

<pre><code>user_profile = UserProfile.objects.get(user=request.user)
</code></pre>

<p>but it would be even easier to do:</p>

<pre><code>user_profile = request.user.userprofile
</code></pre>

<p>Another minor point: you don't show the form, but I assume that the UserProfileForm has <code>latitude</code> and <code>longitude</code> fields on it, in which case there is no need to set them manually. If they're not on the form, why not? By getting them directly from request.POST you're bypassing all the validation the form would do, such as checking that they are valid float values.</p>

python django views nonetype querying