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?

rahnama7m
  • 865
  • 10
  • 38

1 Answers1

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)
rahnama7m
  • 865
  • 10
  • 38
flakavir
  • 190
  • 1
  • 6
  • It raise this error: `TypeError: 'User' object is not iterable` @flakavir – rahnama7m Jun 09 '19 at 08:53
  • oh sorry, change initial with instance UserFormSet = SignUpAdminForm(instance = user) – flakavir Jun 09 '19 at 08:59
  • 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 – rahnama7m Jun 09 '19 at 12:07
  • use initial value if you want... UserFormSet = SignUpAdminForm(instance = user, initial={'bio': user.profile.bio, 'degree': user.profile.degree}) – flakavir Jun 09 '19 at 12:31