How to overwrite on my django models

I want to show the details in user models and I want to allow users to edit it. I am almost done with it. but instead saving edits it tries to save everything and bringing me a error.

This is the function I used for it :

def EditProfile(request):

    if request.method == 'POST':
        form = EditProfileForm(request.POST, instance=request.user.userauthentication)
        form2 = EditProfileForm2(request.POST, instance=request.user)

        username = form2['username'].value()
        email = form2['email'].value()
        auth = form['D_Auth'].value()
        chid = form['D_ChID'].value()

        if username is None:
            messages.success(request, 'You must enter Username')
            return redirect('edit_profile')

        elif email is None:
            messages.success(request, 'You must enter Email')
            return redirect('edit_profile')

        elif User.objects.filter(username=username).exists():
            messages.success(request, 'Username is already taken')
            return redirect('edit_profile')

        elif User.objects.filter(email=email).exists():
            messages.success(request, 'Email is already Taken')
            return redirect('edit_profile')

        elif auth is None:
            messages.success(request, 'Authentication Key Must be Entered')
            return redirect('edit_profile')

        elif chid is None:
            messages.success(request, 'Channel ID Must be Entered')
            return redirect('edit_profile')

        elif UserAuthentication.objects.filter(D_Auth=auth).exists():
            messages.success(request, 'Authentication key in Use')
            return redirect('edit_profile')

        elif form.is_valid() and form2.is_valid():
            userform = form.save()
            customform = form2.save(False)
            customform.user = userform
            customform.save()
            return redirect('Home')
    else:

        form = EditProfileForm(instance=request.user.userauthentication)
        form2 = EditProfileForm2(instance=request.user)
        context = {
            'form':form2,
            'form2':form
        }

    return render(request, 'registration/edit_profile.html', context)

any improvement are also welcomes.

Yes, a submitted form saves the entire object, not just the changed fields.

All of this is unnecessary. Those form fields should be marked as “required” and the tests for those fields belongs in the form, not the view.

Review the Working with forms page.

If there are any field names in common between those two forms, you need to use a prefix for the form.

For more detailed assistance, we’ll need to see your EditProfileForm and EditProfileForm2, and possibly the template (edit_profile.html) as well.

Yes bro i knew that i can use django forms. But while we talk about frontend, editing the django forms is little more complex, else I need to use bootstrap but i dont want to do those stuffs so i created those forms.
my need:
Now I want to show the current user details in that custom form and I want it to update the user profile instead of over writting it. I wish you will understand me.

@KenWhitesell Bro if you need anymore details don’t hesitate to ask bro.

From my previous response:

@KenWhitesell I rewrite the code little bit with the help of this Link.

Now this is my code
views.py

def EditProfile(request):

    if request.method == 'POST':

        username = User.objects.get(username)
        email = request.POST.get('id_email')
        auth = request.POST.get('D_Auth')
        chid = request.POST.get('D_ChID')
        utyp = request.POST.get('U_Type')
        loss = request.POST.get('N_Loss')

        if loss == 'on':
            flos = True
        else:
            flos = False

        if username is None:
            messages.success(request, 'You must enter Username')
            return redirect('edit_profile')

        elif email is None:
            messages.success(request, 'You must enter Email')
            return redirect('edit_profile')

        elif User.objects.filter(username=username).exists():
            messages.success(request, 'Username is already taken')
            return redirect('edit_profile')

        elif User.objects.filter(email=email).exists():
            messages.success(request, 'Email is already Taken')
            return redirect('edit_profile')

        elif auth is None:
            messages.success(request, 'Authentication Key Must be Entered')
            return redirect('edit_profile')

        elif chid is None:
            messages.success(request, 'Channel ID Must be Entered')
            return redirect('edit_profile')

        elif UserAuthentication.objects.filter(D_Auth=auth).exists():
            messages.success(request, 'Authentication key in Use')
            return redirect('edit_profile')

        else:
            user = User.objects.get(username=user)
            userauth_obj = UserAuthentication.objects.update(
                U_User = user,
                defaults = {
                    "D_Auth": auth,
                    "D_ChID": chid,
                    "U_Type": utyp,
                    "N_Loss": flos,
                }
            )
            user_obj = User.objects.update(
                defaults = {
                "username": username,
                "email": email
                }
            )

    return render(request, 'registration/edit_profile.html')

edit_profile.html

{% extends "index.html" %}
{% block content %}

<div class="container">
        <form method="post" class="form" id="form">
        {% csrf_token %}

        <h1>Register</h1>

        {% if messages %}
            {% for message in messages %}
                <div class="alert alert-info">
                    {{ message }}
                </div>
            {% endfor %}
        {% endif %}

        <div class="form-group">
            <label for="username">Username</label>
            <input type="text" name="username" id="username" class="form-control">
        </div>

        <div class="form-group">
            <label for="id_email">Email ID</label>
            <input type="text" name="id_email" id="id_email" class="form-control">
        </div>

        <div class="form-group">
            <label for="D_Auth">Discord Authentication Key</label>
            <input type="text" name="D_Auth" id="D_Auth" class="form-control">
        </div>

        <div class="form-group">
            <label for="D_ChID">Discord Channel Id</label>
            <input type="text" name="D_ChID" id="D_ChID" class="form-control">
        </div>

        <div class="form-group">
            <label for="U_Type">Player Type</label>
            <select name="U_Type" id="U_Type" class="form-control">
                <option value="Normal">Normal Player</option>
                <option value="Patron">Patron Player</option>
            </select>
        </div>

        <div class="form-group">
            <label for="N_Loss">No Loss</label>
            <input type="checkbox" name="N_Loss" id="N_Loss" class="form-control">
        </div>

        <button class="btn btn-success mt-3" type="submit">Submit</button>

        <a href="{% url 'register' %}">Create a Account</a>
        <a href="{% url 'forgetpass' %}">Reset My Password</a>

        </form>
</div>

{% endblock content %}

And now i want to show the details of the user in the corresponding fields in the page.

In your GET section of your code, you’re creating a form from the initial instance of your data.

However, your templates are directly rendering HTML fields and not the fields you’ve defined in your form.

Replace the parts of your template containing individual input fields with your form fields.

Review

and

You’re using UserAuthentication.objects.update( and User.objects.update( when you I believe you wanted UserAuthentication.objects.update_or_create( and User.objects.update_or_create(.

But i don’t want to use django form system

Sorry bro i cant understand what you are telling can you clearly explain what you are doing

But you already are!

From your code:

The entire “form system” as you refer to it is an integrated component of Django. It’s all designed to work together. You can’t just pick and choose the pieces that you think you want to use and ignore the rest without creating a lot of extra work for yourself.

You’re using .update rather than .update_or_create.

            userauth_obj = UserAuthentication.objects.update(
                U_User = user,
                defaults = {
                    "D_Auth": auth,
                    "D_ChID": chid,
                    "U_Type": utyp,
                    "N_Loss": flos,
                }
            )

Should be:

            userauth_obj, created = UserAuthentication.objects.update_or_create(
                U_User = user,
                defaults = {
                    "D_Auth": auth,
                    "D_ChID": chid,
                    "U_Type": utyp,
                    "N_Loss": flos,
                }
            )