I'm having an issue that when a user is submitting changes to their profile, the associated user-table has all fields overwritten with blank values (ie: username, email, first_name...etc)
My site has two types of users with completely different user profile types, for this reason I can't use the user-profiles setting in settings.py, unless someone knows how to make that work?
In my models.py (simplified, but to show the User model was inherited):
class UserProfile(User):
somevar = models.CharField(max_length=30)
objects = UserManager()
class ProfileForm(ModelForm):
class Meta:
model = UserProfile
class UserForm(ModelForm):
password2 = forms.CharField(widget=forms.PasswordInput())
remail = forms.CharField(max_length=75)
class Meta:
model = UserProfile
widgets = {'password': forms.PasswordInput()}
fields = ('email', 'password')
In my views.py:
@login_required
def profile_data(request):
"""
enter user profile data
"""
if request.method == 'POST': # submission / data change
user = UserProfile.objects.get(pk=request.user.id)
form = ProfileForm(request.POST, instance=user)
if form.is_valid():
form.save()
return render(request, 'users/profile_edit.html', {'form': form})
else: # no data entered = fill all users data in
form = ProfileForm(initial=model_to_dict(UserProfile.objects.get(id=request.user.id)))
return render(request, 'users/profile_edit.html', {'form': form})
The problem is that if the form is valid, and the form saves, it affects the user table for that user by making all values blank, since they those attributes contain no data in the form model. I've tried excluding all user-table fields from the ProfileForm, but it seems there should be an easier way.
Any help?