Hi,

I'm just starting out learning Django, trying to cut my teeth by
working on a simple website that involves a user profile.

I've created a ModelForm of the profile model, that instances the
profile of the logged in user, which I then want the user to be able
to update so that they can update their profile information. When I
hit the update button, however, I get a validation error pointing at
the ForeignKey field which links the profile to the User model.

I think the problem is that form.save() is trying to insert another
record, rather than update the current, probably related to the
primary key being automatically incremented, but I can't figure out
how to fix it. I was hoping somebody here might be able to point me in
the right direction.

Relevant code snippets are below:

--------------------

#models.py

from django.db import models
from django.contrib.auth.models import User
from django.forms import ModelForm

class Organisation(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    email = models.EmailField(max_length=50)
    organisation_name = models.CharField(max_length=50)
    club_name = models.CharField(max_length=50)
    paypal_address = models.EmailField(max_length=50)
    phone_number = models.IntegerField()
    coach_company = models.CharField(max_length=50)
    user = models.ForeignKey(User, unique=True, primary_key=True)

class OrganisationForm(ModelForm):
    class Meta:
        model = Organisation

#views.py

from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth.models import User
from django.template import Context, loader
from FanTran.organisations.models import OrganisationForm

def profile(request):
    if request.user.is_authenticated():

        organisation = request.user.get_profile()
        profile_form = OrganisationForm(instance=organisation)

        profile = request.user.get_profile()
        t = loader.get_template('organisations/profile.html')
        c = Context ({
            'profile_form':profile_form,
            'profile':profile,
        })
        return HttpResponse(t.render(c))
    else:
        return HttpResponse('Error: Not a valid user')

def profile_update(request):
    if request.user.is_authenticated():
        if request.method == 'POST':
            form = OrganisationForm(request.POST)
            if form.is_valid():
                form.save()
                return HttpResponseRedirect('accounts/profile/')
            else:
                return HttpResponse(form.errors)
    else:
        return HttpResponseRedirect('/login/')

---------------

Thanks in advance,

Jon

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to