我正在尝试保存Authors模型,但它两次保存到数据库中。 views.py
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PHP Live MySQL Database Search</title>
<style type="text/css">
body{
font-family: Arail, sans-serif;
}
.search-box{
width: 300px;
position: relative;
display: inline-block;
font-size: 14px;
}
.search-box input[type="text"]{
height: 32px;
padding: 5px 10px;
border: 1px solid #CCCCCC;
font-size: 14px;
}
.result{
position: absolute;
z-index: 999;
top: 100%;
left: 0;
}
.search-box input[type="text"], .result{
width: 100%;
box-sizing: border-box;
}
.result p{
margin: 0;
padding: 7px 10px;
border: 1px solid #CCCCCC;
border-top: none;
cursor: pointer;
}
.result p:hover{
background: #f2f2f2;
}
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
var fields = 1;
function add_fields() {
fields++;
var objTo = document.querySelector('.search-box')
var divtest = document.createElement("div");
divtest.innerHTML = '<input type="text" autocomplete="off" placeholder="Étel keresése"/><div class="result"></div>';
objTo.appendChild(divtest)
}
$(document).ready(function(){
$('.search-box input[type="text"]').on("keyup input", function(){
var inputVal = $(this).val();
var resultDropdown = $(this).siblings(".result");
if(inputVal.length){
$.get("backend-search.php", {term: inputVal}).done(function(data){
resultDropdown.html(data);
});
} else{
resultDropdown.empty();
}
});
$(document).on("click", ".result p", function(){
$(this).parents(".search-box").find('input[type="text"]').val($(this).text());
$(this).parent(".result").empty();
});
});
</script>
</head>
<body>
<form action="order-food.php" action="post">
<input type="button" onclick="add_fields();" value="Hozzáad">
<br>
<div class="search-box">
<input type="text" autocomplete="off" placeholder="Étel keresése" />
<div class="result"></div>
</div>
<br>
<input type="submit" value="Rendelés felvétele">
</form>
</body>
</html>
我认为问题出在m_missing_authors吧?
models.py
<?php
$link = mysqli_connect("localhost", "root", "", "etterem");
if($link === false){
die("Nem sikerült a csatlakozás: " . mysqli_connect_error());
}
if(isset($_REQUEST["term"])){
$sql = "SELECT * FROM foods WHERE food_name LIKE ?";
if($stmt = mysqli_prepare($link, $sql)){
mysqli_stmt_bind_param($stmt, "s", $param_term);
$param_term = $_REQUEST["term"] . '%';
if(mysqli_stmt_execute($stmt)){
$result = mysqli_stmt_get_result($stmt);
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo "<p>" . $row["food_name"] . "</p>";
}
} else{
echo "<p>Nincs találat</p>";
}
} else{
echo "Nem sikerült a lehívás: $sql. " . mysqli_error($link);
}
}
mysqli_stmt_close($stmt);
}
mysqli_close($link);
?>
数据库是sqllite3 Django版本是2.2.1
答案 0 :(得分:1)
bulk_create方法在执行查询后自动保存结果。
将代码更改为此:
if missing_authors:
Authors.objects.bulk_create(missing_authors)
''' remove these lines
for m in missing_authors:
m.save()
#not sure what this line is doing exactly, but it might be causing your problem
book.authors.add(*existing_authors, *missing_authors)
'''
如果您可以为author_name列设置unique=True
,请尝试以下操作:
class Authors(models.Model):
author_name = models.CharField(max_length=200, unique=True)
Authors.objects.bulk_create(missing_authors, ignore_conflicts=True)
for m in missing_authors:
m.save()
book.authors.add(*existing_authors, *missing_authors)