出于某种原因,在Django管理员中,它不会让我订购/排序(降序)我的价格字段(使用 list_display )。
可以对其他元素进行排序,但不能对pk外部的元素进行排序。
如何实现
Models.py -------
<?php
include("connect.php");
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
ini_set('session.cookie_lifetime',900);
ini_set('session.gc_maxlifetime',900);
session_start();
if(isset($_POST['searchbtn'])) {
$_SESSION['searchterm']=$_POST['searchterm'];
if(($_SESSION['searchterm'])!="") { header("location:index.php"); }
else { echo "<script> alert('Please enter something to search for') </script>"; }
}
?>
<html>
<head>
<title>1KaraokeDJ.com Search</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container">
<form method="post">
<p><code>
<img src="top1.jpg" /><br>
<?php if ($detect->isMobile()) { echo("Mobile Device Detected"); } else { echo("Desktop Browser Detected"); } ?>
</code></p>
<p>
<?php if(isset($_SESSION['searchterm'])) { ?>
<input name="searchterm" type="search" value="<?php echo $_SESSION['searchterm'];?>" class="search" />
<?php } else { ?>
<input name="searchterm" type="search" placeholder="Enter Search Terms Here" class="search" />
<?php } ?>
</p>
<p>
<input name="searchbtn" type="submit" id="searchbtn" class="searchbutton" value="Artist Search" />
<input name="searchbtn" type="submit" id="searchbtn" class="searchbutton" value="Title Search" />
<input name="searchbtn" type="submit" id="searchbtn" class="searchbutton" value="Disc ID Search" />
</p>
<hr style="width:100%">
<?php if(isset($_SESSION['searchterm'])) {
if(($_SESSION['searchterm']!="")) {
$searchterm=strtoupper($_SESSION['searchterm']);
print_r($_POST);
switch ($_POST['searchbtn']) {
case 'Artist Search':
$searchresults=$db->query("select * from 1KaraokeDJ where Artist like '%$searchterm%' limit 100");
break;
case 'Title Search':
$searchresults=$db->query("select * from 1KaraokeDJ where Title like '%$searchterm%' limit 100");
break;
case 'Disc ID Search':
$searchresults=$db->query("select * from 1KaraokeDJ where Disc like '%$searchterm%' limit 100");
break;
}
if(mysqli_num_rows($searchresults) > 0) {
while($descri=mysqli_fetch_object($searchresults)) { ?>
<div class="reslt">
<h3 id="results">
<?php
echo str_ireplace($searchterm, '<span class="highlight">'.$searchterm."</span>", $descri->Artist);
echo " - ";
echo str_ireplace($searchterm, '<span class="highlight">'.$searchterm."</span>", $descri->Title);
?>
</h3>
<p class="Description">
<?php
echo str_ireplace($searchterm, '<span class="highlight">'.$searchterm."</span>", $descri->Brand);
echo " - ";
echo str_ireplace($searchterm, '<span class="highlight">'.$searchterm."</span>", $descri->Disc);
echo " - ";
echo $descri->Track;
?>
<p>
<hr>
</div>
<?php } ?>
<div class="reslt">
<h3 id="results"><?php echo mysqli_num_rows($searchresults) ?> Results</h3>
<?php if(mysqli_num_rows($searchresults) >= 100) { ?>
<p class="Description highlight">Showing Up To 100 Results<br>Try Refining Your Search</p><hr>
<?php } ?>
</div>
<?php } else { ?>
<div class="reslt">
<h3 id="results">Nothing Found!</h3>
<p class="Description highlight">Try Changing Your Search Terms<p><hr>
</div>
<?php } } } ?>
</form>
<code>This site is in testing... Things may change anytime<br>and many things may not work for a while!</code>
</div>
</body>
</html>
Admin.py --------
class Product(models.Model):
name = models.CharField ("Name", max_length=400)
link = models.URLField("Link", max_length=900)
class Variation(models.Model):
product = models.ForeignKey(Product, blank=False, null=False)
variation = models.CharField (max_length=80, blank=True, null=True)
def __str__(self):
return self.variation
class Price(models.Model):
price = models.DecimalField("Price", decimal_places=2, max_digits=10)
variation = models.ForeignKey(Variation, blank=False, null=False)
updated = models.DateTimeField(auto_now=True)
答案 0 :(得分:0)
您无法尝试使用订购属性将admin_order_field添加到管理员的价格字段中,如下所示:
class ProductAdmin(admin.ModelAdmin):
list_display = ('name', 'price',)
ordering = ('variation__price',)
def price(self, obj):
try:
price = Price.objects.filter(variation__product=obj)[0].price
except (ValueError, IndexError):
None
else:
return '${0}'.format(price)
price.admin_order_field = 'variation__price'