2

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?

devinjames
  • 103
  • 6

1 Answers1

0

This is working as designed. In multiple table inheritance, attributes of parent are made available in child. Child object can overwrite them when saving.

If you want to have different types of users, here is recent question regarding this How to set up Django models with two types of users with very different attributes

Community
  • 1
  • 1
Rohan
  • 52,392
  • 12
  • 90
  • 87
  • Thank-you for your response @Rohan. Is this not possible while having UserProfile inherit from the User model rather than doing a one-to-one reference? It seems to me that site functionality would be lost if not inheriting. – devinjames Oct 22 '12 at 16:28
  • @DevinS, multiple inheritance is same as onetoone relationship. – Rohan Oct 22 '12 at 16:50
  • I did some reading and understand, I was thinking of ForeignKey, since I have not had a reason to use OneToOneField. Thanks! – devinjames Oct 22 '12 at 17:59
  • does this method disable the possibility to include user table fields in the user form for this model? I've noticed that it's not possible to set something like `fields = ('user.email', 'user.password') in the form models meta class. Rather I have to use something along the lines of exclude = ('....extensive user field list'). Is it possible to avoid using the exclusion method? – devinjames Oct 31 '12 at 23:17
  • @DevinS, that is again as expected. By default all fields will be included in the modelform. You can choose using appropriately defining `fields` or `exclude` list. For inherited model, parents fields are added in the form as well, you can exclude them by `exclude=('first_name', 'last_name')`. No `'user.email'` – Rohan Nov 01 '12 at 04:24