Django.fun

Disabling an duplicate database entries || Django

I currently have a settings form that enters their entries into the Django SQLite database. I obviously don't want the user to enter 2 settings with the same name.

I have tried to use a for loop to check if the data exist, but just can't get it right.

Here is my current code for this form:

Views.py:

def newSetting(request):
    form = SettingsForm()

    if request.method == 'POST':
        form = SettingsForm(request.POST)
        if form.is_valid():
            form.save()

    return render(request , 'main/newSetting.html' , {'form':form})

newSetting.html:

{% extends "main/base.html"%}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-wEmeIV1mKuiNpC+IOBjI7aAzPcEZeedi5yW5f2yOq55WWLwNGmvvx4Um1vskeMj0" crossorigin="anonymous">

<style >
  .row_container{
    line-height: 500%;
  }
</style>
{% block content %}
<form class="form-group mt-4" action="" method="post">
  {% csrf_token %}
  {{ form.Complex }}
  <br>
  <br>
  <div class="row_container" name='TRB-YTD'>
    <div class="row mb-.1">
      <div class="col" style="left-align">
          {{ form.Trial_balance_Year_to_date }}
      </div>
      <div class="col-11">
        <p> Trial balance YTD</p>
      </div>
    </div>
  </div>
  <div class="row_container" name="TRB-MONTH">
    <div class="row ">
      <div class="col" style="left-align">
          {{ form.Trial_balance_Monthly }}
      </div>
      <div class="col-11">
        <p> Trial balance Monthly</p>
      </div>
    </div>
  </div>
  <div class="row_container" name="IS-YTD">
    <div class="row ">
      <div class="col" style="left-align">
          {{ form.Income_Statement_Year_to_date }}
      </div>
      <div class="col-11">
        <p> Income Statement YTD</p>
      </div>
    </div>
  </div>
  <div class="row_container" name="IS-MONTHLY">
    <div class="row ">
      <div class="col" style="left-align">
          {{ form.Income_Statement_Monthly }}
      </div>
      <div class="col-11">
        <p> Income Statement Monthly</p>
      </div>
    </div>
  </div>
  <div class="row_container" name="AGE-ANALYSIS">
    <div class="row ">
      <div class="col" style="left-align">
          {{ form.Age_Analysis }}
      </div>
      <div class="col-11">
        <p> Age Analysis</p>
      </div>
    </div>
  </div>
  <div class="row_container" name="BAL-SHEET">
    <div class="row ">
      <div class="col" style="left-align">
          {{ form.Balance_Sheet }}
      </div>
      <div class="col-11">
        <p> Balance Sheet</p>
      </div>
    </div>
  </div>
  <div class="row_container" name="REP-MAIN-GL">
    <div class="row ">
      <div class="col" style="left-align">
          {{ form.Repair_and_Maintenance_General_Ledger }}
      </div>
      <div class="col-11">
        <p> Repair & Maintenance GL</p>
      </div>
    </div>
  </div>
  <div class="row_container" name="MAJOR-CAPTAL-GL">
    <div class="row ">
      <div class="col" style="left-align">
          {{ form.Mayor_capital_Items_General_Ledger }}
      </div>
      <div class="col-11">
        <p> Major Capital Items GL</p>
      </div>
    </div>
  </div>

  <button type="submit" name="button" class="btn btn-success "> Submit </button>
</form>
{% endblock %}

Model.py:

class SettingsClass(models.Model):
    Complex = models.CharField(choices=complex_list , max_length =  15 ,default='1')
    Trial_balance_Year_to_date= models.BooleanField(default = False)
    Trial_balance_Monthly=models.BooleanField(default = False)
    Income_Statement_Year_to_date=models.BooleanField(default = False)
    Income_Statement_Monthly=models.BooleanField(default = False)
    Age_Analysis=models.BooleanField(default = False)
    Balance_Sheet=models.BooleanField(default = False)
    Repair_and_Maintenance_General_Ledger=models.BooleanField(default = False)
    Mayor_capital_Items_General_Ledger=models.BooleanField(default = False)
    def __str__(self):
        return (self.Complex + ' Settings')

I want to check for duplicate database entries with the "Complex" field

Answers: 1

Answered by KyleStranger, Sept. 14, 2021, 10:46 a.m.

As per @IainShelvington 's comment.

The solution is adding unique=True to your key fields setting when declaring it on Model.py

Like so:

  Complex = models.CharField(choices=complex_list , max_length =  15 ,default='1' , unique=True)