When should user.get_profile be used in django?

I saw another answer here and other places on the Internet that recommend using user.get_profile when expanding the django built-in user. I did not do this in the example below. The functionality seems to work fine, but is there a drawback in not using user.get_profile ()?

model

class UserProfile(models.Model):
    user = models.ForeignKey(User, primary_key=True)
    quote = models.CharField('Favorite quote', max_length =  200, null=True, blank=True)
    website = models.URLField('Personal website/blog', null=True, blank=True)

class UserProfileForm(ModelForm):
    class Meta:
        model = UserProfile
        fields = ('quote', 'website')

View

@login_required 
def user_profile(request):
    user = User.objects.get(pk=request.user.id)
    if request.method == 'POST':
        upform = UserProfileForm(request.POST)
        if upform.is_valid():
            up = upform.save(commit=False)
            up.user = request.user
            up.save()
            return HttpResponseRedirect('/accounts/profile')
    else:
        upform = UserProfileForm()
    return render_to_response('reserve/templates/edit_profile.html', locals(), context_instance=RequestContext(request))
+5
source share
1 answer

The code works the way you wrote it, but since you are not passing an instance of your model, this is a bit unusual, so another Django developer may be needed to develop what happens.

, , , . .

upform = UserProfileForm(instance=user.get_profile())

, user_profile, . , user , .

user.get_profile() , , . UserProfile , instance=UserProfile.objects.get(user=user).

+3

All Articles