Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango-profiles custom create/edit modelForm not saving properly
    text
    copied!<p>Summary:</p> <pre><code>u = self.instance.user </code></pre> <p>in</p> <pre><code>def save(self, *args, **kwargs): u = self.instance.user u.first_name = self.cleaned_data['first_name'] u.last_name = self.cleaned_data['last_name'] u.save() return super(ProfileForm, self).save(*args, **kwargs) </code></pre> <p>is causing a problem because self.instance doesn't exist. But yet this is how it is done in other examples, where it seems to work. What am I missing?</p> <p>Read on for more info -></p> <p>I am using both django-registration and django-profiles. For the purposes of just getting it to work, I have not added any extra fields to the profile model (the one that extends User). So far it looks like this:</p> <pre><code>class sumaConnectUser(models.Model): user = models.ForeignKey(User) def __unicode__(self): return self.user.first_name + " " + self.user.last_name def get_absolute_url(self): return ('profiles_profile_detail', (), { 'username': self.user.username }) get_absolute_url = models.permalink(get_absolute_url) </code></pre> <p>My understanding is as of now, my user "profile" should just include the fields that come with the contrib.auth model User. ( first name, last name etc)</p> <p>In my urls.py, I pass in my custom form for the creation and edit of the profiles-</p> <pre><code>(r'^profiles/edit', 'profiles.views.edit_profile', {'form_class': ProfileForm, }), (r'^profiles/create', 'profiles.views.create_profile', {'form_class': ProfileForm, }), (r'^profiles/', include('profiles.urls')), </code></pre> <p>Finally, here is my profile form-</p> <pre><code>from suma.sumaconnect.models import sumaConnectUser from django import forms from django.contrib.auth.models import User class ProfileForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(ProfileForm, self).__init__(*args, **kwargs) try: self.fields['first_name'].initial = self.instance.user.first_name self.fields['last_name'].initial = self.instance.user.last_name except User.DoesNotExist: pass first_name = forms.CharField(label="First Name") last_name = forms.CharField(label="Last Name") class Meta: exclude = ('user',) model = sumaConnectUser def save(self, *args, **kwargs): u = self.instance.user u.first_name = self.cleaned_data['first_name'] u.last_name = self.cleaned_data['last_name'] u.save() return super(ProfileForm, self).save(*args, **kwargs) </code></pre> <p>My goal is to allow the user to edit their first name and last name as part of the profile edit, but not their username and password.</p> <p>I thought about replacing</p> <pre><code>u = self.instance.user </code></pre> <p>with</p> <pre><code>u = User.objects.get(user = self.cleaned_data['username']) </code></pre> <p>but this would require me to include a username = forms.CharField on the page which I do not want to display. As far as I understand, when I come to the create profile or edit profile page, I should be automatically editing the profile associated with the user which I am logged in as.</p> <p>By the time I come to this create or edit user page, the user model already exists, but the profile doesn't. Is this the cause of the problem? I think I am misunderstanding something major, and I would greatly appreciate any pointers as to where I am going wrong. Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload