如何停止重复输入Django模型

时间:2018-11-12 10:36:39

标签: django-forms

我想停止重复输入表单字段。如果我输入了已经存储到数据库中的相同数据,那么它会显示一条错误消息或不会被存储。请帮助我。我是Django的新手。

Form.html文件

example.com/api?detail=small,big,small

Model.py文件

<form class="well form-horizontal" method="post" action="{% url 'manager_add' %}">
            {% csrf_token %}
              <fieldset>
                 <div class="form-group">
                    <label class="col-md-4 control-label">Manager Name</label>
                    <div class="col-md-6 inputGroupContainer">
                       <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span><input id="fullName" name="mname" placeholder="Full Name" class="form-control" required="true" value="" type="text"></div>
                    </div>
                 </div>
                 <div class="form-group">
                    <label class="col-md-4 control-label">Select Department</label>
                    <div class="col-md-6 inputGroupContainer">
                       <div class="input-group">
                          <span class="input-group-addon" style="max-width: 100%;"><i class="glyphicon glyphicon-list"></i></span>
                          <select class="selectpicker form-control" name="dprtmnt">
                             <option>Department 1</option>
                             <option>Department 2</option>
                             <option>Department 3</option>
                             <option>Department 4</option>
                          </select>
                       </div>
                    </div>
                 </div>
                  <div class="form-group">
                    <label class="col-md-4 control-label">Email</label>
                    <div class="col-md-6 inputGroupContainer">
                       <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span><input id="email" name="email" placeholder="Email" class="form-control" required="true" value="" type="text"></div>
                    </div>
                 </div>
                 <div class="form-group">
                    <label class="col-md-4 control-label">Contact</label>
                    <div class="col-md-6 inputGroupContainer">
                       <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-earphone"></i></span><input id="phoneNumber" name="phone" placeholder="Phone Number" class="form-control" required="true" value="" type="text"></div>
                    </div>
                 </div>
                 <button>Submit</button>
              </fieldset>
           </form>

View.py文件

class Manager(models.Model):
    mname = models.CharField(max_length=20)
    dprtmnt = models.CharField(max_length=10)
    email = models.CharField(max_length=50)
    phone = models.CharField(max_length=20)

    def __str__(self):
        return self.mname

1 个答案:

答案 0 :(得分:0)

您可以使用Manager.objects.get_or_create(),请参阅文档的this part

您还可以简单地检查数据库中是否已存在条目:

try:
    Manager.objects.get(mname = mname, dprtmnt = dprtmnt, email = email, phone = phone)
    # Do things if manager exists
except:
    # Create your new manager