DJANGO:指的是 id

发布于 2024-11-09 18:22:58 字数 394 浏览 0 评论 0原文

这是给我带来问题的一点:错误“QuerySet”对象没有属性“地址”

        bdns = Business.objects.filter(name='slow')
        addx = bdns.address
        addr = Address.objects.get(id=addx)

我该怎么办?

我的商业模式:

class Business(models.Model): 
    phone = PhoneNumberField()
    address = models.ForeignKey(Address)
    name = models.CharField(max_length=64)

Here's the bit that's giving me an issue: error 'QuerySet' object has no attribute 'address'

        bdns = Business.objects.filter(name='slow')
        addx = bdns.address
        addr = Address.objects.get(id=addx)

What should I do?

My model for Business:

class Business(models.Model): 
    phone = PhoneNumberField()
    address = models.ForeignKey(Address)
    name = models.CharField(max_length=64)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

热鲨 2024-11-16 18:22:58

查询集是一个集合,即使该集合只包含一个元素。当您执行 Model.objects.filter() 时,它会返回一个查询集。

如果您想返回单个对象,请使用 Model.objects.get()。

因此,为了您的目的:

bdns = Business.objects.filter(name='slow') # returns a collection
b = dbns[0] # get the first one
the_address = b.address # the address

# or...
try:
    bdns = Business.objects.get(name='slow') # get single instance
except Business.DoesNotExist:
    bdns = None # instance didnt exist, assign None to the variable
except Business.MultipleObjectsReturned:
    bdns = None # the query returned a collection

if bdns is not None:
    the_address = bdns.address

# the_address is an instance of an Address, so no need to do the lookup with the id

print the_address.id # 7
print the_address.street # 17 John St
print the_address.city # Melbourne

A queryset is a collection, even if that collection only contains one element. When you do Model.objects.filter(), it returns a queryset.

If you want to return a single object, use Model.objects.get().

So, for your purposes:

bdns = Business.objects.filter(name='slow') # returns a collection
b = dbns[0] # get the first one
the_address = b.address # the address

# or...
try:
    bdns = Business.objects.get(name='slow') # get single instance
except Business.DoesNotExist:
    bdns = None # instance didnt exist, assign None to the variable
except Business.MultipleObjectsReturned:
    bdns = None # the query returned a collection

if bdns is not None:
    the_address = bdns.address

# the_address is an instance of an Address, so no need to do the lookup with the id

print the_address.id # 7
print the_address.street # 17 John St
print the_address.city # Melbourne
呢古 2024-11-16 18:22:58
    bdns = Business.objects.filter(name='slow')

返回一个 QuerySetBusiness 对象的集合),您需要迭代以获取带有地址的每个元素。

addr = Address.objects.get(id=addx)

应该有效

    bdns = Business.objects.filter(name='slow')

Returns you a QuerySet (the collection of Business objects) You need to iterate to get each element with address.

addr = Address.objects.get(id=addx)

Should work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文