我在django比较新, 我有以下模特定义,
<?php
if ($_POST['user'] == "ppks82" &&
md5($_POST['pass']) =="9f5tf71ca91p693a36c403d75a377864") {
session_start();
$_SESSION['user'] = $_POST['user'];
header('Location: index.php');
exit();
}
else {
header('Location: login.php');
}
?>
说,我们在特定商品标题下有5条优惠行。
现在来自class Project(models.Model):
offer_header = models.ForeignKey(
'offers.OfferHeader',
verbose_name=_('Offer Header'),
help_text=_('From which offer this project started'),
)
class OfferLine(models.Model):
offer_header = models.ForeignKey(
OfferHeader,
verbose_name=_('Offer Header'),
)
对象,我怎样才能获得该优惠标题的优惠行,django是否会针对这种情况提供任何查询?
答案 0 :(得分:0)
首先为反向关系添加related_name(为了便于访问):
class OfferLine(models.Model):
offer_header = models.ForeignKey(
OfferHeader,
verbose_name=_('Offer Header'),
related_name='lines'
)
然后从project
访问相关对象,如下所示:
project.offer_header.lines.all()
# This will give you all offer lines for that offer header
您将获得该项目的商品标题的商品行查询集。如果您需要特定的约束,如果您有约束,那么您也可以过滤结果并获得您想要的结果:
project.offer_header.lines.filter(...)
Django docs related objects的更多信息。