Very good hint, urukay! I was a bit scared to use forms.Form because I
didn't know how to do it. But it was straight forward and works like a
charm! I'm getting my data saved working with two differend models.
Just awesome :-).
One last thing there (and after that I will clean up my code and write
a blog article about it to save others some headache [but even if the
last days were a bit frustrating sometimes, I have to admit that it
definitely was good for my understanding of python and django]). Ok
getting to the "problem" (not even slightly comparable to my previous
problems): When the user visites his profile page the data from the
database should be already filled in. How would I do this? Probably
this will be quite easy. Hopefully the two models to get the pre-fill
data from won't be a problem. And another small question: Do you know
a better way for getting the user object in the save method other than
the way I did (just passed it as an argument)?

Ok here is now my code that made it work:

>>> model <<<
class CustomUserProfile(models.Model):
    GENDER_CHOICES = (
        ('-', _('not specified')),
        ('M', _('male')),
        ('F', _('female'))
    )
    user = models.ForeignKey(User, unique=True, editable=False)
    birthdate = models.DateTimeField(_('birthdate'), blank=True)
    gender = models.CharField(_('gender'), max_length=1,
choices=GENDER_CHOICES)
    country = models.CharField(_('country'), max_length=30,
blank=True)
    city = models.CharField(_('city'), max_length=30, blank=True)

    def get_absolute_url(self):
        return ('profiles_profile_detail', (), { 'username':
self.user.username })
    get_absolute_url = models.permalink(get_absolute_url)

>>> form <<<
class CustomUserForm(forms.Form):
    first_name = forms.CharField(required=False)
    last_name = forms.CharField(required=False)
    gender = forms.CharField(
        label = _('Gender'),
        widget = forms.Select
(choices=CustomUserProfile.GENDER_CHOICES))
    birthdate = forms.DateField(required=False)
    country = forms.CharField(required=False)
    city = forms.CharField(required=False)

    def save(self, user):
        user_obj = User.objects.get(pk=user.id)
        user_obj.first_name = self.cleaned_data['first_name']
        user_obj.last_name = self.cleaned_data['last_name']

        try:
            profile_obj = user.get_profile()
        except ObjectDoesNotExist:
            profile_obj = CustomUserProfile()
            profile_obj.user = user
        profile_obj.birthdate = self.cleaned_data['birthdate']
        profile_obj.gender = self.cleaned_data['gender']
        profile_obj.country = self.cleaned_data['country']
        profile_obj.city = self.cleaned_data['city']

        profile_obj.save()
        user_obj.save()

>>> view <<<
def edit_profile(request, form_class=CustomUserForm,
    success_url="/profiles/edit/complete/"):

    if request.method == 'POST':
        form = form_class(data=request.POST)
        if form.is_valid():
            form.save(user=request.user)
            return HttpResponseRedirect(success_url)
    else:
        form = form_class()
    return render_to_response("profiles/edit_profile.html",
        {'form':form},
        context_instance=RequestContext(request))

I hope that code is readable enough to get it :-).

Alex

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

Reply via email to