Ad

Multiple Instance In One Django Form For Editing Profile Model Objects

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?

Ad

Answer

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)
Ad
source: stackoverflow.com
Ad