Error linking django to legacy database

261 views Asked by At

I am trying to show database fields from dcm4chee pacs database (postgresql) in my browser-run django app. I have used inspectdb to create a model - relevant bits below:

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

However when i try to add to the app from django admin, i get "maximum recursion depth exceeded" (line 469 is repeated hundreds of time). Brief traceback:

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

The original file which built the postgresql database is copy-pasted below. It is quite lengthy, but most tables give similar error when accessed through django admin and space is limited here; you can look at table 'study' for an example:

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);

Apologies if relevant bits have been left out. Will be happy to provide further info.

1

There are 1 answers

2
sk1p On BEST ANSWER

I think pk is handled as a special case; you shouldn't name your field pk. Try:

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

The problem is that _set_pk_val, which is used as the setter for the pk property, just calls itself if the attribute name for the primary key, _meta.pk.attname, is 'pk'