English 中文(简体)
django/python:有多个表格的原材料价格
原标题:django/python: raw sql with multiple tables

我需要在多个桌子上进行原木。 然后我得出结果。 表格一:

sql = "select * from my_table"
results = my_table.objects.raw(sql)

关于多个表格,我正在做:

sql = "select * from my_table, my_other_table where ...."
results = big_model.objects.raw(sql)

但是,我是否确实需要建立一个包含我可能需要的所有领域的表/模型/等级大体。 我永远不会在这种“表”中储存任何数据。

ADDED:

我有一张表格。 我的名单上有一席。 这些定义见《示范公约》py。 我的列名对我的使用者具有外国钥匙,表明谁制造了列名。

页: 1

"select user_name, listing_text from my_listings, my_users where my_users.id = my_listings.my_user_id". 

我希望这能够产生一种结果,使我能够利用这一结果,使我的网页在django。

问题在于:我是否必须树立一个包含现场用户——姓名和列名——内容的模型? 还是有某种更好的方法,仍然使用原材料(选用,从哪里)? 当然,我的实际询问比这个例子更为复杂。 (我界定的模式)。 p成为数据库的实际表格,因此使用了模型/表术语。 不能确定其他方式,令人不安。 我使用的是原始数据,因为我发现,纸面表格的参考资料只使用简单的数据模型。

最佳回答
  1. This works. Don t know why it didn t before :( From Dennis Baker s comment:

你们确实需要一个与其所有领域的模式,你们刚刚需要第一个模式和领域。 你确实需要拥有独一无二的名称,而且只要我知道,你就应该使用“表名.实地名称”,以确保你拥有所有独特的领域。 我做了一些相当复杂的询问,有5+表格,总是将它们与单一模式联系起来。 ——

页: 1 另一种解决办法是使用 cur。 然而,必须把 cur子从les子清单改为dict。 我确信,使用探测器有更清洁的方法,但这一功能行之有效。 它包含一个星体,即原壳体,并将一个清单作为模板提供和使用。

from django.db import connection, transaction

def sql_select(sql):
    cursor = connection.cursor()
    cursor.execute(sql)
    results = cursor.fetchall()
    list = []
    i = 0
    for row in results:
        dict = {} 
        field = 0
        while True:
           try:
                dict[cursor.description[field][0]] = str(results[i][field])
                field = field +1
            except IndexError as e:
                break
        i = i + 1
        list.append(dict) 
    return list  
问题回答

你们不需要一种模式,包括你希望从原 s中返回的田地。 如果你真的有一个模式,即你希望从自己的原产地回来,那么你就可以将自己的原始 output果绘制成这一模式,否则,你就可以使用,完全围绕模型。





相关问题
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 ]="...

热门标签