English 中文(简体)
外在问题
原标题:foreignkey problem

你们有这个模式:

class Category(models.Model):
      node_id = models.IntegerField(primary_key = True)
      type_id = models.IntegerField(max_length = 20)
      parent_id = models.IntegerField(max_length = 20)
      sort_order = models.IntegerField(max_length = 20)
      name = models.CharField(max_length = 45)
      lft = models.IntegerField(max_length = 20)
      rgt = models.IntegerField(max_length = 20)
      depth = models.IntegerField(max_length = 20)
      added_on = models.DateTimeField(auto_now = True)
      updated_on = models.DateTimeField(auto_now = True)
      status = models.IntegerField(max_length = 20)
      node = models.ForeignKey(Category_info, verbose_name =  Category_info , to_field =  node_id 

The important part is the foreignkey. When I try:

Category.objects.filter(type_id = 15, parent_id = offset, status = 1)

我发现的错误比重的类别,因为本应退还不止一个类别。 但是,我要过滤另一个领域的成果,即类型(从第二个模型中)

这里是:

class Category_info(models.Model):
      objtree_label_id = models.AutoField(primary_key = True)
      node_id = models.IntegerField(unique = True)
      language_id = models.IntegerField()
      label = models.CharField(max_length = 255)
      type_id = models.IntegerField()

从1到5年不等。 我完全试图只取得一种结果,即补贴类型为1。

这里我想的是:

SELECT c.*, ci.*
FROM category c
JOIN category_info ci ON (c.node_id = ci.node_id)
WHERE c.type_id = 15 AND c.parent_id = 50 AND ci.type_id = 1

任何帮助都是GREATLY。

关于

最佳回答

Oke

选择与(......)相关联,加上强调物良好......

问题回答

在一个相关的表格中过滤田地,使用双层计分。 查阅有关<代码>Category 的物体,type_id 的相关<代码>Category_info 反对:

Category.objects.filter(node__type_id=15)

Django随后将自动地理解,你在任何表格<代码>/<>代码>上重新提及<代码> 类型_id领域。

因此,它仍然致力于解决我的问题。 让我尝试作更多的解释。

让我们说:

SELECT c.*, ci.*
FROM category c
JOIN category_info ci ON (c.node_id = ci.node_id)
WHERE c.type_id = 15 AND c.parent_id = 50 

两行将返回。 两者都是相同的,但从“信息表”类中得出的“id field”类型有两种类型:1和2。 如果我补充一下 s——ci.type_id = 1,我将得出正确的结果。 但是,从我所尝试的情况来看,即便是在双重指责的情况下,它仍然在Django恢复两行。

我现在谈谈:

Category.objects.filter(type_id = 15, parent_id = offset, status = 1, node__type_id = 1)

如Node__类型_id = 1 代表“ci.type_id = 1”。 但是,这仍然有两行。 当我从我的模型定义中删除“现场”时,它将传递错误数据,因为数据因违约而将其与主要关键人物联系在一起。 之后,我试图通过把另一个过滤器连接起来来过滤数据,但是仍然胜过。

这里是ug,或许有助于:

Caught an exception while rendering: get() returned more than one Category_info -- it returned 2! Lookup parameters were { node_id__exact : 5379L}

它仍然看着它只想看一看 no子,而不要看 type。

......





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签