gpt4 book ai didi

python - Django 1.3 匹配查询不存在

转载 作者:行者123 更新时间:2023-12-01 06:05:03 27 4
gpt4 key购买 nike

我对这个错误有疑问,希望有人像我一样遇到过这个错误。尝试使用 Models.ForeignKey 字段时,我在模型上收到此错误。我可以验证使用外键的表是否存在,以及 fk 引用的表是否存在。如果我使用 CharField,我可以让模型正常工作,但这并不有趣。

这里的表格(使用 Postgresql 9.0)不是我设计的,所以如果你不喜欢它,请不要对我太挑剔,尽管我不能保证我会设计得更好:

claim 状态

     Column      |            Type             |                               Modifiers                                
-----------------+-----------------------------+------------------------------------------------------------------------
claim_no | character varying(30) | not null
claim_status | character varying(15) |
userid | character varying(40) | not null
note | character varying(255) |
time | timestamp without time zone | default now()
claim_status_id | integer | not null default nextval('claim_status_claim_status_id_seq'::regclass)
Indexes:
"claim_status_pkey" PRIMARY KEY, btree (claim_status_id)
"ndx_claim_status_claim_no" btree (claim_no) CLUSTER
"ndx_claim_status_claim_status" btree (claim_status)
"ndx_claim_status_date_time" btree (date("time"))
"ndx_claim_status_time" btree ("time")
"ndx_claim_status_upper_userid" btree (upper(userid::text))
"ndx_claim_status_userid" btree (userid)
Foreign-key constraints:
"$1" FOREIGN KEY (claim_status) REFERENCES lk_claim_status(claim_status)
"claim_status_claim_no_fkey" FOREIGN KEY (claim_no) REFERENCES claim(claim_no) ON UPDATE CASCADE

lk_claim_status

        Column        |         Type          |   Modifiers   
----------------------+-----------------------+---------------
claim_status | character varying(15) | not null
description_internal | character varying(35) | not null
description_web | character varying(35) | not null
display_insured_web | boolean | default false
display_rep_web | boolean | default false
Indexes:
"lk_claim_status_pkey" PRIMARY KEY, btree (claim_status)
Referenced by:
TABLE "claim_status" CONSTRAINT "$1" FOREIGN KEY (claim_status) REFERENCES lk_claim_status(claim_status)

我的模型:

from django.db import models
from test.django.common.models.claim.lk_claim_status import LkClaimStatus

class ClaimStatus(models.Model):
claim_no = models.CharField(max_length=40)
# the foreign key does not work here, you get matching query error for some reason.
claim_status = models.ForeignKey(LkClaimStatus, db_column='claim_status')
#claim_status = models.CharField(max_length=15)
userid = models.CharField(max_length=40)
note = models.CharField(max_length=255)
time = models.DateTimeField(auto_now=True)
claim_status_id = models.AutoField(primary_key=True)
class Meta:
db_table = u'claim_status'

--结束claim_status.py--开始lk_claim_status.py

from django.db import models

class LkClaimStatus(models.Model):
claim_status = models.CharField(max_length=15, primary_key=True)
description_internal = models.CharField(max_length=35)
description_web = models.CharField(max_length=35)
display_insured_web = models.BooleanField()
display_rep_web = models.BooleanField()
class Meta:
db_table = u'lk_claim_status'

我还没有在任何页面中使用过,但刚刚使用manage.py shell对其进行了测试

这是我的测试

from test.django.common.models.claim.claim_status import ClaimStatus
from test.django.common.models.claim.lk_claim_status import LkClaimStatus
status_list = ClaimStatus.objects.filter(claim_no='TEST')
for status in status_list:
try:
print status.claim_status.claim_status
except LkClaimStatus.DoesNotExist:
print "Got a blank one"

最后一个测试只打印 except print 语句。如果我使用 ClaimStatus 模型中的 CharField 选项,我可以打印 status.claim_status 并从数据库表中获取值。

from test.django.common.models.claim.claim_status import ClaimStatus
from test.django.common.models.claim.lk_claim_status import LkClaimStatus
status_list = ClaimStatus.objects.filter(claim_no='TEST')
for status in status_list:
print status.claim_status.claim_status

通过上述测试,我得到了这个:

>>> for status in status_list:
... print status.claim_status.claim_status
...
Traceback (most recent call last):
File "<console>", line 2, in <module>
File "/usr/lib/python2.5/site-packages/django/db/models/fields/related.py", line 315, in __get__
rel_obj = QuerySet(self.field.rel.to).using(db).get(**params)
File "/usr/lib/python2.5/site-packages/django/db/models/query.py", line 349, in get
% self.model._meta.object_name)
DoesNotExist: LkClaimStatus matching query does not exist.

如果表不存在或字段不存在,我会理解这一点,但它们存在吗?我还可以通过测试验证任一表中的 Claim_status 字段都没有空值。

最佳答案

我绝对可以在这里看到您的模型存在一个问题。当您定义名为“claim_status”的 ForegnKey 字段时,Django 将自动在模型实例上创建一个名为“claim_status_id”的属性,该属性将用于存储“claim_status”数据库列的原始值。但是,您已经有一个同名的主键字段,这可能会导致冲突。

我不确定解决此问题的官方方法是什么,但您可以尝试子类化ForeignKey并重写其get_attname()方法以返回其他内容。

关于python - Django 1.3 匹配查询不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8483981/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com