通常,如果我为此编写一条sql语句,我会这样做,
SELECT * FROM (django_baseaccount LEFT JOIN django_account ON django_baseaccount.user_id = django_account.baseaccount_ptr_id)
LEFT JOIN django_address ON django_account.baseaccount_ptr_id = django_address.user_id;name 如何将其放入使用API查询数据库的Djagno方法中。
TradeDownloads.objects.filter(online=1)[:6]我的模型基本帐户
class BaseAccount(models.Model):
user = models.ForeignKey(User, unique=True)
def __unicode__(self):
"""
Return the unicode representation of this customer, which is the user's
full name, if set, otherwise, the user's username
"""
fn = self.user.get_full_name()
if fn:
return fn
return self.user.username
def user_name(self):
"""
Returns the full name of the related user object
"""
return self.user.get_full_name()
def email(self):
"""
Return the email address of the related user object
"""
return self.user.email账号
class Account(BaseAccount):
"""
The account is an extension of the Django user and serves as the profile
object in user.get_profile() for shop purchases and sessions
"""
telephone = models.CharField(max_length=32)
default_address = models.ForeignKey(Address, related_name='billing_account', blank=True, null=True)
security_question = models.ForeignKey(SecurityQuestion)
security_answer = models.CharField(max_length=200)
how_heard = models.CharField("How did you hear about us?", max_length=100)
feedback = models.TextField(blank=True)
opt_in = models.BooleanField("Subscribe to mailing list", help_text="Please tick here if you would like to receive updates from %s" % Site.objects.get_current().name)
temporary = models.BooleanField()
def has_placed_orders(self):
"""
Returns True if the user has placed at least one order, False otherwise
"""
return self.order_set.count() > 0
def get_last_order(self):
"""
Returns the latest order that this customer has placed. If no orders
have been placed, then None is returned
"""
try:
return self.order_set.all().order_by('-date')[0]
except IndexError:
return None
def get_currency(self):
"""
Get the currency for this customer. If global currencies are enabled
(settings.ENABLE_GLOBAL_CURRENCIES) then this function will return
the currency related to their default address, otherwise, it returns
the site default
"""
if settings.ENABLE_GLOBAL_CURRENCIES:
return self.default_address.country.currency
return Currency.get_default_currency()
currency = property(get_currency)
def get_gateway_currency(self):
"""
Get the currency that an order will be put through protx with. If protx
currencies are enabled (settings.ENABLE_PROTX_CURRENCIES), then the
currency will be the same returned by get_currency, otherwise, the
site default is used
"""
if settings.ENABLE_PROTX_CURRENCIES and settings.ENABLE_GLOBAL_CURRENCIES:
return self.currency
return Currency.get_default_currency()
gateway_currency = property(get_gateway_currency)地址
class Address(models.Model):
"""
This class encapsulates the data required for postage and payment mechanisms
across the site. Each address is associated with a single store account
"""
trade_user = models.BooleanField("Are you a stockist of N Products", help_text="Please here if you are a Stockist")
company_name = models.CharField(max_length=32, blank=True)
line1 = models.CharField(max_length=200)
line2 = models.CharField(max_length=200, blank=True)
line3 = models.CharField(max_length=200, blank=True)
city = models.CharField(max_length=32)
county = models.CharField(max_length=32)
postcode = models.CharField(max_length=12)
country = models.ForeignKey(Country)
account = models.ForeignKey('Account')
class Meta:
"""
Django meta options
verbose_name_plural = "Addresses"
"""
verbose_name_plural = "Addresses"
def __unicode__(self):
"""
The unicode representation of this address, the postcode plus the county
"""
return ', '.join((self.postcode, str(self.county)))
def line_list(self):
"""
Return a list of all of this objects address lines that are not blank,
in the natural order that you'd expect to see them. This is useful for
outputting to a template with the aid of python String.join()
"""
return [val for val in (self.line1, self.line2, self.line3, self.city, self.county, self.postcode, self.country.name) if val]发布于 2009-12-02 19:29:33
“通常情况下,如果我正在编写sql语句”
欢迎使用ORM。您不是在编写SQL,因此请从问题中删除这一点。永远不要发布SQL并询问如何将SQL转换为ORM。翻译SQL会限制您的学习能力。别再这么做了。
写下预期的结果。
看起来您正在获取所有的Account对象。句号。在视图函数或模板中的某些点上,您也希望获得一个Address。
for a in Account.objects.all():
a.default_address # this is the address that SQL brought in via a "join".就这样。请实际执行Django教程中的所有示例。实际输入示例中的代码,看看它是如何工作的。
所有的"join“操作都是SQL变通方法。它们是一种奇怪的SQL-ism,与底层对象无关。因此,停止使用SQL术语来描述您想要的内容。
发布于 2009-12-02 18:28:15
对于复杂的查询,Django提供了一种干净的方法来回退到原生SQL,请参阅官方文档:Performing raw SQL queries
发布于 2009-12-02 19:06:04
忘了SQL吧。您希望通过此查询实现什么目标?您想对结果做什么?
你还没有发布你的模型。它们是否定义了外键?你能不能只做一个简单的查询,然后使用select_related()来获取连接的对象?
已编辑以添加给定the previous time you asked this question的答案有什么问题
再次编辑了,但每个人都向你展示了如何通过外键获取项目!忘了id吧,你不需要它。如果您有一个Account对象a,只需执行a.default_address即可获得相关的实际Address对象。如果这不起作用,那么您没有发布正确的模型,因为这肯定会与您发布的模型一起工作。
https://stackoverflow.com/questions/1831980
复制相似问题