[Django]-Django related models and UpdateView fields

2👍

Thanks for the replies! However, since I couldn’t figure out how to make this work and thought using two tables eventually resulted in too much clutter to my taste, I finally went with the easier route and subclassed AbstractUser:

# models.py
class ForumUser(AbstractUser):
    subscribeToMails = models.BooleanField(default=True)
    [...]

# views.py
class EditUser(LoginRequiredMixin, UpdateView):
    model = ForumUser
    fields = ('email', 'emailVisible', 'subscribeToMails', 'mpPopupNotif',
              'mpEmailNotif', 'avatar', 'quote', 'website')
    template_name = 'user/edit.html'
    success_url = reverse_lazy('forum:welcome')

    def get_object(self):
        return ForumUser.objects.get(username=self.request.user)

I only had to change my registration form:

# forms.py
class RegisterForm(UserCreationForm):
    email = forms.EmailField(required=True)

    class Meta:
        model = ForumUser
        fields = ('username', 'email', 'password1', 'password2')

    def clean_email(self):
        "Ensure registered emails are unique."
        email = self.cleaned_data.get('email')
        username = self.cleaned_data.get('username')
        if email and ForumUser.objects.filter(email=email).exclude(
                username=username).count():
            raise forms.ValidationError('Email address already in use.')
        return email

    def clean_username(self):
        """
        UserCreationForm method where mentions of the User model are replaced
        by the custom AbstractUser model (here, ForumUser).
        https://code.djangoproject.com/ticket/19353#no1
        and https://docs.djangoproject.com/en/1.7/_modules/django/contrib/
        auth/forms/#UserCreationForm
        """
        username = self.cleaned_data["username"]
        try:
            ForumUser.objects.get(username=username)
        except ForumUser.DoesNotExist:
            return username
        raise forms.ValidationError(
            self.error_messages['duplicate_username'],
            code='duplicate_username',
        )

1👍

Use this solution:

mix both User and UserSettings in a form like this:

class EmployeeEditForm(forms.ModelForm):
    #fields from User model that you want to edit
    first_name = forms.CharField(required=False, label=_('First Name'))
    last_name = forms.CharField(required=False, label=_('Last Name'))

    class Meta:
        model = UserSettings
        fields = ('first_name', 'last_name', 'subscribeToMails')

You can access to User and UserSettings object in views.py like this:

user = request.user
usersettings = user.usersettings

Now you can edit User object like this:

user.first_name = request.POST['first_name']
user.last_name = request.POST['last_name']
user.save()

And edit UserSettings like this:

usersettings.subscribeToMails = request.POST['subscribeToMails']
usersettings.save() 

-1👍

Formsets is the best way to go about it.

https://docs.djangoproject.com/en/dev/topics/forms/formsets/

Leave a comment