Error: DoesNotExist: matching query does not exist.

I want to save user details from a custom form which has OneToOne connection with User Model. While i try to save it I am getting this error.

UserAuthentication matching query does not exist

The below line is the line from which i getting the above error

userauth_obj = UserAuthentication.objects.get(U_User=user_obj)

def UserAuthform(request):

    if request.method == 'POST':

        user = request.user.username
        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 auth is None:
            messages.success(request, 'Authentication Key Must be Entered')
            return redirect('UserAuthForm')

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

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

        elif utyp is None:
            messages.success(request, 'User Type Must Be Selected')
            return redirect('UserAuthForm')

            user_obj = User.objects.get(username=user)

            userauth_obj = UserAuthentication.objects.get(U_User=user_obj)

            userauth_obj.D_Auth = auth
            userauth_obj.D_ChID = chid
            userauth_obj.U_Type = utyp
            userauth_obj.N_Loss = loss

            return redirect('Home')

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

class UserAuthentication(models.Model):
    U_User = models.OneToOneField(
        primary_key= True,

    D_Auth = models.CharField(max_length=100, unique=True)
    D_ChID = models.PositiveIntegerField(null=False)
    U_Type = models.CharField(max_length=7)
    N_Loss = models.BooleanField(null=False, default=False)
    U_PreM = models.BooleanField(null=False, default=False)

    def __str__(self):
        return self.U_User.username


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

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


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

        <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 class="form-group">
            <label for="D_ChID">Discord Channel Id</label>
            <input type="text" name="D_ChID" id="D_ChID" class="form-control">

        <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>

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

        <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>


{% endblock content %}

Hi @GoAmeer030, the clue is in the error message. UserAuthentication matching query does not exist. The query is .get(U_User=user_obj). Breaking it down it means that there is no UserAuthentication instance that has the propert U_User set to the user_obj instance. This likely means that a user was created, but the corresponding UserAuthentication was not. I’m not entirely sure of the purpose of this view, but you could handle it with update_or_create:

userauth_obj, created = UserAuthentication.objects.update_or_create(
            "D_Auth":  auth,
            "D_ChID":  chid,
            "U_Typ":  utyp,
            "N_Loss":  loss,
1 Like

Thanks bro it works perfectly fine. Please take a look on this too Link. And Please tell where you find these functions and their uses. Thankyou

For me it’s been reading the docs occasionally combined with repeated usage of the functions that caused me to remember that they exist.