Django:通过外键字段的 id 查询的正确方法是什么?
Django: What is the correct way to query by foreign key field's id?
我有两个models
:
class Organization(models.Model):
title = models.CharField(max_length=100)
class Folder(models.Model):
organization = models.ForeignKey("Organization",related_name='folders')
title = models.CharField(max_length=50)
现在我想用 organization id
过滤 folder
。所以我尝试了:
Folder.objects.filter(organization= 1)
Folder.objects.filter(organization_id= 1)
Folder.objects.filter(organization__id= 1)
Folder.objects.filter(organization__pk= 1)
Folder.objects.filter(organization= Organization.objects.get(id=1))
信不信由你returns都一样。
所以有人知道通过外键字段的 id 查询的正确方法是什么吗?
更新
但是当尝试通过以下方式创建 folder
时:
Folder.objects.create(organization__id=1,title='hello')
出现错误:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/suhail/.virtualenvs/heybadges/local/lib/python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/suhail/.virtualenvs/heybadges/local/lib/python2.7/site-packages/django/db/models/query.py", line 370, in create
obj = self.model(**kwargs)
File "/home/suhail/.virtualenvs/heybadges/local/lib/python2.7/site-packages/django/db/models/base.py", line 452, in __init__
raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
TypeError: 'organization__id' is an invalid keyword argument for this function
但 Folder.objects.create(organization_id=1,title='hello')
工作正常。
Django docs说大多数情况下你应该使用Folder.objects.filter(organization__pk=1)
。
回复更新:
可能 Folder.objects.create(organization_id=1,title='hello')
有效,因为 Django appends "_id" to the field name to create its database column name.
我有两个models
:
class Organization(models.Model):
title = models.CharField(max_length=100)
class Folder(models.Model):
organization = models.ForeignKey("Organization",related_name='folders')
title = models.CharField(max_length=50)
现在我想用 organization id
过滤 folder
。所以我尝试了:
Folder.objects.filter(organization= 1)
Folder.objects.filter(organization_id= 1)
Folder.objects.filter(organization__id= 1)
Folder.objects.filter(organization__pk= 1)
Folder.objects.filter(organization= Organization.objects.get(id=1))
信不信由你returns都一样。
所以有人知道通过外键字段的 id 查询的正确方法是什么吗?
更新
但是当尝试通过以下方式创建 folder
时:
Folder.objects.create(organization__id=1,title='hello')
出现错误:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/suhail/.virtualenvs/heybadges/local/lib/python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/suhail/.virtualenvs/heybadges/local/lib/python2.7/site-packages/django/db/models/query.py", line 370, in create
obj = self.model(**kwargs)
File "/home/suhail/.virtualenvs/heybadges/local/lib/python2.7/site-packages/django/db/models/base.py", line 452, in __init__
raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
TypeError: 'organization__id' is an invalid keyword argument for this function
但 Folder.objects.create(organization_id=1,title='hello')
工作正常。
Django docs说大多数情况下你应该使用Folder.objects.filter(organization__pk=1)
。
回复更新:
可能 Folder.objects.create(organization_id=1,title='hello')
有效,因为 Django appends "_id" to the field name to create its database column name.