Django Forms: Foreign Key in Hidden Field


My form:

class PlanForm(forms.ModelForm):    
    owner = forms.ModelChoiceField(label="",

    class Meta:
        model = Plan

Owner, in the model, is a ForeignKey to a Profile.

When I set this form, I set the value of "owner" to be a Profile object.

But when this comes out on the form, it seems to contain the name of the Profile like this:

<input type="hidden" name="owner" value="phil" id="id_owner" />

When the form is submitted and gets back to my I try to handle it like this:

    form = PlanForm(request.POST)
    if form.is_valid():                
        plan =
        return HttpResponseRedirect('/plans/%s' # Redirect after POST

However, what I get is a type-conversion error as it fails to turn the string "phil" (the user's name that was saved into the "owner" field) into an Int to turn it into the ForeignKey.

So what is going on here. Should a ModelForm represent a foreign key as a number and transparently handle it? Or do I need to extract the id myself into the owner field of the form? And if so, how and when do I map it back BEFORE I try to validate the form?

12/28/2018 5:16:42 AM

Accepted Answer

I suspect that the __unicode__ method for the Profile model instance, or the repr thereof is set to return a value other than For example, I just set this up:

class Profile(models.Model):
    name = models.CharField('profile name', max_length=10)

    def __unicode__(self):
        return u'%d' %

class Plan(models.Model):
    name = models.CharField('plan name', max_length=10)
    profile = models.ForeignKey(Profile, related_name='profiles')

    def __unicode__(self):

class PlanForm(forms.ModelForm):
    profile = forms.ModelChoiceField(queryset=Profile.objects.all(),

    class Meta:
        model = Plan

def add_plan(request):

    if request.method == 'POST':
        return HttpResponse(request.POST['profile'])

    profile = Profile.objects.all()[0]
    form = PlanForm(initial={'profile':profile})
    return render_to_response('add_plan.html',

With that, I see PlanForm.profile rendered thus in the template:

<input type="hidden" name="profile" value="1" id="id_profile" />
3/7/2009 2:40:39 PM


This might actually be a security hole.

Suppose a malicious attacker crafted a POST (say, by using XmlHttpRequest from FireBug) and set the profile term to some wacky value, like, your profile ID. Probably not what you wanted?

If possible, you may want to get the profile from the request object itself, rather than what's being submitted from the POST values.

form = PlanForm(request.POST)
if form.is_valid():
    plan =
    plan.owner = request.user.get_profile()
    form.save_m2m() # if neccesary

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow