2014-01-05 64 views
0

我想在瀏覽器運行的django應用程序中顯示來自dcm4chee pacs數據庫(postgresql)的數據庫字段。我已經使用inspectdb創建模型 - 下面的相關位:將django鏈接到遺留數據庫時出錯

class Study(models.Model): 


    pk = models.IntegerField(primary_key=True) 
    patient_fk = models.ForeignKey(Patient, null=True, db_column='patient_fk', blank=True) 
    description = models.CharField(max_length=400, blank=True) 

    class Meta: 
     db_table = 'study' 

    def __unicode__(self): 
     return u"%s" % self.pk 

然而,當我嘗試添加到從Django管理應用程序,我得到「超過最大遞歸深度」(行469重複幾百次) 。簡要回顧:

Traceback: 

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response 
    115.       response = callback(request, *callback_args, **callback_kwargs) 
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in wrapper 
    372.     return self.admin_site.admin_view(view)(*args, **kwargs) 
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view 
    91.      response = view_func(request, *args, **kwargs) 
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/cache.py" in _wrapped_view_func 
    89.   response = view_func(request, *args, **kwargs) 
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/sites.py" in inner 
    202.    return view(request, *args, **kwargs) 
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapper 
    25.    return bound_func(*args, **kwargs) 
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view 
    91.      response = view_func(request, *args, **kwargs) 
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in bound_func 
    21.     return func(self, *args2, **kwargs2) 
File "/usr/local/lib/python2.7/dist-packages/django/db/transaction.py" in inner 
    223.     return func(*args, **kwargs) 
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in add_view 
    1022.    form = ModelForm(initial=initial) 
File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in __init__ 
    240.    self.instance = opts.model() 
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in __init__ 
    405.     setattr(self, field.attname, val) 
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in _set_pk_val 
    469.   return setattr(self, self._meta.pk.attname, value) 
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in _set_pk_val 
    469.   return setattr(self, self._meta.pk.attname, value) 
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in _set_pk_val 

構建postgresql數據庫的原始文件複製粘貼如下。這是相當長的,但大多數表格給出類似的錯誤,當通過Django管理訪問和空間是有限的,你可以看看錶'學習'的例子:

CREATE TABLE ae (
    pk    SERIAL8 NOT NULL CONSTRAINT ae_pk PRIMARY KEY, 
    aet    TEXT NOT NULL, 
    hostname   TEXT NOT NULL, 
    port    INTEGER NOT NULL, 
    cipher_suites  TEXT, 
    pat_id_issuer  TEXT, 
    acc_no_issuer  TEXT, 
    user_id   TEXT, 
    passwd   TEXT, 
    fs_group_id  TEXT, 
    ae_group   TEXT, 
    ae_desc   TEXT, 
    wado_url   TEXT, 
    station_name  TEXT, 
    institution  TEXT, 
    department  TEXT, 
    installed   BOOLEAN NOT NULL, 
    vendor_data  BYTEA 
); 
CREATE UNIQUE INDEX aet ON ae(aet); 
CREATE INDEX hostname ON ae(hostname); 
CREATE INDEX ae_group ON ae(ae_group); 

CREATE TABLE code (
    pk    SERIAL8 NOT NULL CONSTRAINT code_pk PRIMARY KEY, 
    code_value  TEXT NOT NULL, 
    code_designator TEXT NOT NULL, 
    code_version  TEXT, 
    code_meaning  TEXT 
); 
CREATE UNIQUE INDEX code_value ON code(code_value,code_designator,code_version); 

CREATE TABLE issuer (
    pk    SERIAL8 NOT NULL CONSTRAINT issuer_pk PRIMARY KEY, 
    entity_id   TEXT, 
    entity_uid  TEXT, 
    entity_uid_type TEXT 
); 
CREATE UNIQUE INDEX entity_id ON issuer(entity_id); 
CREATE UNIQUE INDEX entity_uid ON issuer(entity_uid,entity_uid_type); 

CREATE TABLE patient (
    pk    SERIAL8 NOT NULL CONSTRAINT patient_pk PRIMARY KEY, 
    merge_fk   INT8, 
    pat_id   TEXT, 
    pat_id_issuer  TEXT, 
    pat_name   TEXT, 
    pat_fn_sx   TEXT, 
    pat_gn_sx   TEXT, 
    pat_i_name  TEXT, 
    pat_p_name  TEXT, 
    pat_birthdate  TEXT, 
    pat_sex   TEXT, 
    pat_custom1  TEXT, 
    pat_custom2  TEXT, 
    pat_custom3  TEXT, 
    created_time  TIMESTAMP, 
    updated_time  TIMESTAMP, 
    pat_attrs   BYTEA, 
FOREIGN KEY (merge_fk) REFERENCES patient(pk) 
); 
CREATE INDEX pat_merge_fk ON patient(merge_fk); 
CREATE INDEX pat_id ON patient(pat_id, pat_id_issuer); 
CREATE INDEX pat_name ON patient(pat_name); 
CREATE INDEX pat_fn_sx ON patient(pat_fn_sx); 
CREATE INDEX pat_gn_sx ON patient(pat_gn_sx); 
CREATE INDEX pat_i_name ON patient(pat_i_name); 
CREATE INDEX pat_p_name ON patient(pat_p_name); 
CREATE INDEX pat_birthdate ON patient(pat_birthdate); 
CREATE INDEX pat_sex ON patient(pat_sex); 
CREATE INDEX pat_custom1 ON patient(pat_custom1); 
CREATE INDEX pat_custom2 ON patient(pat_custom2); 
CREATE INDEX pat_custom3 ON patient(pat_custom3); 

CREATE TABLE other_pid (
    pk    SERIAL8 NOT NULL CONSTRAINT other_pid_pk PRIMARY KEY, 
    pat_id   TEXT NOT NULL, 
    pat_id_issuer  TEXT NOT NULL 
); 
CREATE UNIQUE INDEX other_pat_id ON other_pid(pat_id, pat_id_issuer); 

CREATE TABLE rel_pat_other_pid (
    patient_fk  INT8, 
    other_pid_fk  INT8, 
FOREIGN KEY (patient_fk) REFERENCES patient(pk), 
FOREIGN KEY (other_pid_fk) REFERENCES other_pid(pk) 
); 
CREATE INDEX other_pid_pat_fk ON rel_pat_other_pid(patient_fk); 
CREATE INDEX pat_other_pid_fk ON rel_pat_other_pid(other_pid_fk); 

CREATE TABLE study (
    pk    SERIAL8 NOT NULL CONSTRAINT study_pk PRIMARY KEY, 
    patient_fk  INT8, 
    accno_issuer_fk INT8, 
    study_iuid  TEXT NOT NULL, 
    study_id   TEXT, 
    study_datetime TIMESTAMP, 
    accession_no  TEXT, 
    ref_physician  TEXT, 
    ref_phys_fn_sx TEXT, 
    ref_phys_gn_sx TEXT, 
    ref_phys_i_name TEXT, 
    ref_phys_p_name TEXT, 
    study_desc  TEXT, 
    study_custom1  TEXT, 
    study_custom2  TEXT, 
    study_custom3  TEXT, 
    study_status_id TEXT, 
    mods_in_study  TEXT, 
    cuids_in_study TEXT, 
    num_series  INTEGER NOT NULL, 
    num_instances  INTEGER NOT NULL, 
    ext_retr_aet  TEXT, 
    retrieve_aets  TEXT, 
    fileset_iuid  TEXT, 
    fileset_id  TEXT, 
    availability  INTEGER NOT NULL, 
    study_status  INTEGER NOT NULL, 
    checked_time  TIMESTAMP, 
    created_time  TIMESTAMP, 
    updated_time  TIMESTAMP, 
    study_attrs  BYTEA, 
FOREIGN KEY (patient_fk) REFERENCES patient(pk), 
FOREIGN KEY (accno_issuer_fk) REFERENCES issuer(pk) 
); 
CREATE INDEX patient_fk ON study(patient_fk); 
CREATE INDEX accno_issuer_fk ON study(accno_issuer_fk); 
CREATE UNIQUE INDEX study_iuid ON study(study_iuid); 
CREATE INDEX study_id ON study(study_id); 
CREATE INDEX study_datetime ON study(study_datetime); 
CREATE INDEX accession_no ON study(accession_no); 
CREATE INDEX ref_physician ON study(ref_physician); 
CREATE INDEX ref_phys_fn_sx ON study(ref_phys_fn_sx); 
CREATE INDEX ref_phys_gn_sx ON study(ref_phys_gn_sx); 
CREATE INDEX ref_phys_i_name ON study(ref_phys_i_name); 
CREATE INDEX ref_phys_p_name ON study(ref_phys_p_name); 
CREATE INDEX study_desc ON study(study_desc); 
CREATE INDEX study_custom1 ON study(study_custom1); 
CREATE INDEX study_custom2 ON study(study_custom2); 
CREATE INDEX study_custom3 ON study(study_custom3); 
CREATE INDEX study_status_id ON study(study_status_id); 
CREATE INDEX study_checked ON study(checked_time); 
CREATE INDEX study_created ON study(created_time); 
CREATE INDEX study_updated ON study(updated_time); 
CREATE INDEX study_status ON study(study_status); 

道歉,如果相關位被遺漏了。將很樂意提供進一步的信息。

回答

1

我認爲pk是作爲一個特例處理;你不應該命名你的領域pk。嘗試:

id = models.IntegerField(primary_key=True, db_column="pk") 

的問題是,_set_pk_val,這是用來作爲二傳手的pk財產,只稱自己如果主鍵,_meta.pk.attname屬性名,是「PK」

+0

由於水桶。我知道這是在網上發佈這是正確的! – Maelstorm

+0

偉大的解決方案,我一直在這一段時間:) – Oran