亲爱的人们试图帮助他人,
我试图找出如何让Django为我做一个连接,而无需编写自定义SQL.
假设我有以下型号
class Parent(models.Model):
name = models.CharField()
children = models.ManyToManyField(Child, through="Parent_Child", related_name="parents")
class Parent_Child(models.Model):
parent = models.ForeignKey(Parent, related_name='attached_children')
child = models.ForeignKey(Child, related_name='attached_parents')
class Child(models.Model):
name = models.CharField()
toys = models.ManyToManyField(Toy, hrough="Child_Toy", related_name="toy_owners")
class Child_Toy(models.Model):
child = models.ForeignKey(Child, related_name='attached_toys')
toy = models.ForeignKey(Toy, related_name='toy_owner')
class Toy(models.Model):
name = models.CharField
Run Code Online (Sandbox Code Playgroud)
父母可以有多个孩子.一个孩子可以有多个父母.孩子可以拥有多个玩具.玩具可以由多个孩子拥有.
我想获得父母子女拥有的所有玩具清单.
所以,我可以做以下事情:
parent.children.all()
和child.toys.all()
我想做的就是parent.children.toys.all()
当我尝试这样做时,我得到:AttributeError: 'ManyRelatedManager' object has no attribute 'toys'
.我理解错误 - parent.children
返回多个记录.这是预料之中的.我无法弄清楚的是如何给Django提示我希望它为其查询添加一个额外的连接.
有没有办法在Django中进行此连接,或者我是否需要转到自定义SQL才能执行此操作?
请注意:以上只是为了说明我的问题,我使用的实际模型并不相关.我的问题是试图弄清楚如何在Django中加入多个M2M关系,而不必诉诸SQL.
我提前感谢您的帮助.谢谢!
我仍然对代理模型与django中的超类之间的关系感到困惑.我现在的问题是如何从已经检索到的超类实例中获取代理模型的实例?
所以,让我说我有:
class Animal(models.Model):
type = models.CharField(max_length=20)
name = models.CharField(max_length=40)
class Dog(Animal):
class Meta:
proxy = True
def make_noise(self):
print "Woof Woof"
Class Cat(Animal):
class Meta:
proxy = True
def make_noise(self):
print "Meow Meow"
animals = Animal.objects.all()
for animal in animals:
if (animal.type == "cat"):
animal_proxy = # make me a cat
elif (animal.type == "dog"):
animal_proxy = # make me a dog
animal_proxy.make_noise()
Run Code Online (Sandbox Code Playgroud)
好.那么......"让我成为一只猫"的内容不需要回溯到数据库,例如:
animal_proxy = Cat.objects.get(id=animal.id)
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法从Animal的一个实例创建一个Cat实例,我知道它是一只猫?
我正在尝试使用jquery ui draggable/droppable拖放到textarea中.我将textarea设置为droppable.我可以在丢弃发生时触发事件,但我无法弄清楚如何准确确定textarea中"drop"发生在哪个位置.
基本上我想在文本区域的精确位置插入文本.
谷歌现在没有帮助我,这让我感到难过.
例:
<html><head><title>Drag Drop Test</title>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/themes/base/jquery-ui.css" type="text/css" media="all" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
$( "#catalog li" ).draggable({
appendTo: "body",
helper: "clone"
});
$( "#mytext" ).droppable({
accept: ":not(.ui-sortable-helper)",
drop: function( event, ui ) {
console.log(ui.position);
// figure out where in the textarea this is
// drop in some string
}
});
});
</script>
</head>
</body>
<div id="products">
<div id="catalog">
<h3><a href="#">T-Shirts</a></h3>
<div>
<ul>
<li>Lolcat Shirt</li>
<li>Cheezeburger Shirt</li>
<li>Buckit Shirt</li>
</ul>
</div> …
Run Code Online (Sandbox Code Playgroud)