DJANGO:指的是 id
这是给我带来问题的一点:错误“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查询集是一个集合,即使该集合只包含一个元素。当您执行
Model.objects.filter()
时,它会返回一个查询集。如果您想返回单个对象,请使用 Model.objects.get()。
因此,为了您的目的:
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:
返回一个
QuerySet
(Business
对象的集合),您需要迭代以获取带有地址的每个元素。应该有效
Returns you a
QuerySet
(the collection ofBusiness
objects) You need to iterate to get each element with address.Should work