2

I used the profile method to be able to customize the user model. I also use this site to create a registration form in which both the user and the profile can be created at the same time.

Now I want to write edite_user view. In this question I also find out that must use modelformset_factory to create edit_viewfor this goal.

This is my files:

#models.py 
class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.TextField(max_length=500)
    degree = models.CharField()

#forms 
class SignUpAdminForm(UserCreationForm):
    bio = forms.CharField(widget=forms.Textarea)
    degree = forms.CharField()
    #Also_their_is_another_fields_here
    class Meta:
        model = User
        fields = (
            'username',
            'email',
            'password1',
            'password2',
            'bio',
            'degree',
        )
#Views.py
def add_user(request):
    if request.method == 'POST':
        form = SignUpAdminForm(request.POST)
        if form.is_valid():
            user = form.save()
            user.refresh_from_db()
            user.profile.bio = form.cleaned_data['bio']
            user.profile.bio = form.cleaned_data['degree']
            user.save()
            return redirect('view_user')
    else:
        form = SignUpAdminForm()
    template = 'add_user.html'
    context = {'form': form}
    return render(request, template, context)

def edit_user(request, id):
    user = get_object_or_404(User, id=id)
    if request.method == "POST":
        UserFormSet = modelformset_factory(user, form=SignUpAdminForm)
        if UserFormSet.is_valid():
            edit_user = UserFormSet.save(commit=False)
            edit_user.save()
            return redirect('view_user')
    else:
        UserFormSet = modelformset_factory(User, form=SignUpAdminForm)
    template = 'edit_user.html'
    context = {'UserFormSet': UserFormSet}
    return render(request, template, context)

But this view did not work.I Also see these links: 1,2,3 and 4; but they couldn't help me and I didn't understand what to do. Can you help me write the editing view?

1 Answer 1

1

i think you need to set initial user

how about this?

def edit_user(request, id):
    user = get_object_or_404(User, id=id)
    if request.method == "POST":
        UserFormSet = SignUpAdminForm(request.POST, instance = user)
        if UserFormSet.is_valid():
            edit_user = UserFormSet.save(commit=False)
            edit_user.profile.bio = form.cleaned_data['bio']
            edit_user.profile.degree = form.cleaned_data['degree']
            edit_user.save()
            return redirect('view_user')
    else:
        UserFormSet = SignUpAdminForm(instance = user,initial={
            'bio':profile.bio,
            'degree': profile.degree})
    template = 'edit_user.html'
    context = {'UserFormSet': UserFormSet}
    return render(request, template, context)
Sign up to request clarification or add additional context in comments.

4 Comments

It raise this error: TypeError: 'User' object is not iterable @flakavir
oh sorry, change initial with instance UserFormSet = SignUpAdminForm(instance = user)
I did work. But still exist one another problem for bio and degree field of Profile model: it doesn't show previous value that I set when I created user. how can handel this? @flakavir
use initial value if you want... UserFormSet = SignUpAdminForm(instance = user, initial={'bio': user.profile.bio, 'degree': user.profile.degree})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.