I'm learning Django. I have two models as follows:
我正在学习Django。我有两个模型如下:
class IdentificationAddress(models.Model):
id_ident_address = models.AutoField(primary_key=True)
ident = models.ForeignKey('Ident', models.DO_NOTHING, db_column='ident')
address = models.TextField()
time = models.DateTimeField()
class Meta:
managed = False
db_table = 'identification_address'
class IdentC(models.Model):
id_ident = models.AutoField(primary_key=True)
ident = models.TextField(unique=True)
name = models.TextField()
class Meta:
managed = False
db_table = 'ident'
I am using the Django debugger:
我正在使用Django调试器:
python manage.py shell
and import the model
并导入模型
from invent.models import IdentC as identity
from invent.models import IdentificationAddress as ident_Address
I can access the name of a specific identity
我可以访问特定身份的名称
indentity.objects.filter(ident='00LD').values('name')
it returns
<QuerySet [{'name': u'Joss'}]>
Then I can access information of a specific address:
然后我可以访问特定地址的信息:
ident_Address.objects.filter(address='LOC23').values().last()
it returns
{'times': u'{"2017-07-16"}', u'ident_id': u'00LD', 'address': u'LOC23', 'id_ident_address': 217}
but I get an error when I try to use any identity
但是当我尝试使用任何身份时,我收到错误
ident_Address.objects.filter(ident='00LD').values()
or
ident_Address.objects.filter(ident_id='00LD').values()
error:
ValueError: invalid literal for int() with base 10: '00LD'
If I try this in postgres
如果我在postgres中尝试这个
SELECT * FROM identification_address WHERE ident= '00LD'
I don't have any problem.
我没有任何问题。
I appreciate any help.
我感谢任何帮助。
Thank you in advance NOTE: For the sake of clarity, I've changed the name of the Ident model to IdentC
提前谢谢注意:为了清楚起见,我已将Ident模型的名称更改为IdentC
3 个解决方案
#1
0
You try to match '00LD' with the field id
of Ident
.
Or in your first example you wrote :indentity.objects.filter(ident='00LD').values('name')
'00LD' match with ident
field of Ident
您尝试将'00LD'与Ident的字段ID匹配。或者在你写的第一个例子中:indentity.objects.filter(ident ='00LD')。values('name')'00LD'与Ident的ident字段匹配
So you can filter like this :ident_Address.objects.filter(ident__ident='00LD').values()
First ident represent your model, and the second one, the field of Ident
.
所以你可以像这样过滤:ident_Address.objects.filter(ident__ident ='00LD')。values()第一个ident表示你的模型,第二个表示Ident的字段。
If you want to filter ident_Address by Ident name : ident_Address.objects.filter(ident__name='Joss').values()
如果你想通过Ident名称过滤ident_Address:ident_Address.objects.filter(ident__name ='Joss')。values()
#2
0
In Django, when you want to access some field in ForeignKey table, you should use __
(Double underscore). For your case, it will be
在Django中,当您想要访问ForeignKey表中的某个字段时,您应该使用__(双下划线)。对于你的情况,它会
ident_Address.objects.filter(ident__id_ident='00LD')
#3
0
Thanks guys. Sorry @Exprator I'm learning how to use the answer field, as well. So I started debugging how Django queries are translated to SQL queries. Then I found the solution:
多谢你们。对不起@Exprator我也在学习如何使用答案字段。所以我开始调试如何将Django查询转换为SQL查询。然后我找到了解决方案:
ident_Address.objects.filter(ident__id_ident__iexact='00LD').values()
Where first ident means field ident in IdentificationAddress model and id_ident field in identC model
其中first ident表示IdentificationAddress模型中的字段ident和identC模型中的id_ident字段
#1
0
You try to match '00LD' with the field id
of Ident
.
Or in your first example you wrote :indentity.objects.filter(ident='00LD').values('name')
'00LD' match with ident
field of Ident
您尝试将'00LD'与Ident的字段ID匹配。或者在你写的第一个例子中:indentity.objects.filter(ident ='00LD')。values('name')'00LD'与Ident的ident字段匹配
So you can filter like this :ident_Address.objects.filter(ident__ident='00LD').values()
First ident represent your model, and the second one, the field of Ident
.
所以你可以像这样过滤:ident_Address.objects.filter(ident__ident ='00LD')。values()第一个ident表示你的模型,第二个表示Ident的字段。
If you want to filter ident_Address by Ident name : ident_Address.objects.filter(ident__name='Joss').values()
如果你想通过Ident名称过滤ident_Address:ident_Address.objects.filter(ident__name ='Joss')。values()
#2
0
In Django, when you want to access some field in ForeignKey table, you should use __
(Double underscore). For your case, it will be
在Django中,当您想要访问ForeignKey表中的某个字段时,您应该使用__(双下划线)。对于你的情况,它会
ident_Address.objects.filter(ident__id_ident='00LD')
#3
0
Thanks guys. Sorry @Exprator I'm learning how to use the answer field, as well. So I started debugging how Django queries are translated to SQL queries. Then I found the solution:
多谢你们。对不起@Exprator我也在学习如何使用答案字段。所以我开始调试如何将Django查询转换为SQL查询。然后我找到了解决方案:
ident_Address.objects.filter(ident__id_ident__iexact='00LD').values()
Where first ident means field ident in IdentificationAddress model and id_ident field in identC model
其中first ident表示IdentificationAddress模型中的字段ident和identC模型中的id_ident字段