Opened 19 years ago

Closed 19 years ago

Last modified 18 years ago

#521 closed enhancement (fixed)

Multilevel foreign key ordering causes error in Admin changeview

Reported by: andreas Owned by: Adrian Holovaty
Component: Metasystem Version:
Severity: blocker Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Ordering by a foreign key of a model which is itself ordered by another foreign key causes a "column does not exist"-error in Admin changeview.

Code:

class ParentClass(meta.Model):
    somevar        = meta.CharField(maxlength=100)
    def __repr__(self):
        return self.somevar
    class META:
        admin      = meta.Admin(list_display = ('somevar'))
        ordering   = ['somevar']
 
class FirstChild(meta.Model):
    theparent      = meta.ForeignKey(ParentClass)
    myvar          = meta.CharField(maxlength=100)
    def __repr__(self):
        return self.myvar
    class META:
        admin = meta.Admin(list_display = ('myvar', 'theparent'))
        ordering = ['theparent']

class ChildsChild(meta.Model):
    firstchild     = meta.ForeignKey(FirstChild)
    myownvar       = meta.CharField(maxlength=100)
    def __repr__(self):
        return self.myownvar
    class META:
        admin = meta.Admin(list_display = ('myownvar', 'firstchild'))
        ordering   = ['firstchild']

Error when viewing Admin changeview of ChildsChild:

There's been an error:

Traceback (most recent call last):

  File "C:\Programme\Python24\lib\site-packages\django\core\handlers\base.py", line 64, in get_response
    response = callback(request, **param_dict)

  File "C:\Programme\Python24\lib\site-packages\django\views\admin\main.py", line 183, in change_list
    result_list = lookup_mod.get_list(**lookup_params)

  File "C:\Programme\Python24\lib\site-packages\django\utils\functional.py", line 3, in _curried
    return args[0](*(args[1:]+moreargs), **dict(kwargs.items() + morekwargs.items()))

  File "C:\Programme\Python24\lib\site-packages\django\core\meta\__init__.py", line 1123, in function_get_list
    return list(function_get_iterator(opts, klass, **kwargs))

  File "C:\Programme\Python24\lib\site-packages\django\core\meta\__init__.py", line 1106, in function_get_iterator
    cursor.execute("SELECT " + (kwargs.get('distinct') and "DISTINCT " or "") + ",".join(select) + sql, params)

  File "C:\Programme\Python24\lib\site-packages\django\core\db\base.py", line 10, in execute
    result = self.cursor.execute(sql, params)

ProgrammingError: ERROR:  column myproject_firstchilds.theparent does not exist

Change History (6)

comment:1 by jd@…, 19 years ago

Severity: normalblocker

I believe I have ran into a problem relating to this as well. The below is a lot of information but
I hope it helps resolve the issue. I assigned this as a blocker as it essentially makes my app unusable
if it is indeed a bug. Take the following two models:

class Company(meta.Model):
   name = meta.CharField(maxlength=150, help_text='The company name')
   type = meta.ForeignKey(CompanyType, core='true', verbose_name='type', help_text='Vendor/Lead')
   status = meta.ForeignKey(CompanyStatu, help_text='Inactive/Active', verbose_name='status')
   def __repre__(selef):
      return self.name
   class META:
      db_table = 'company'
      admin = meta.Admin(
         list_display = ('name','type','status'),)

And:

class Address(meta.Model):
   company_id = meta.ForeignKey(Company, core='true', edit_inline=meta.STACKED, num_in_admin=1, help_text='This column does exist!',related_name='id',db_column='company_id')
   address_type = meta.ForeignKey(AddressType, core='true', help_text='This is the type of address')
   address1 = meta.CharField(maxlength=150)
   address2 = meta.CharField(maxlength=150)
   address3 = meta.CharField(maxlength=150)
   city = meta.ForeignKey(City)  
   state_province = meta.ForeignKey(StateProvince)
   country = meta.ForeignKey(Country)
   mail_code = meta.ForeignKey(MailCode)
   class META:
      db_table = 'addresses'
      admin = meta.Admin(
         list_display = ('id','address_type','address1','city'),
      )

When trying to add a new address I receive the following:

here's been an error:

Traceback (most recent call last):

  File "/usr/lib/python2.4/site-packages/django-1.0.0-py2.4.egg/django/core/handlers/base.py", line 71, in get_response
    response = callback(request, **param_dict)

  File "/usr/lib/python2.4/site-packages/django-1.0.0-py2.4.egg/django/contrib/admin/views/decorators.py", line 49, in _checklogin
    return view_func(request, *args, **kwargs)

  File "/usr/lib/python2.4/site-packages/django-1.0.0-py2.4.egg/django/contrib/admin/views/main.py", line 814, in add_stage
    elif not f.blank and ((isinstance(f.rel, meta.ManyToOne) and not f.rel.raw_id_admin) or f.choices) and len(manipulator[f.name].choices) == 2:

  File "/usr/lib/python2.4/site-packages/django-1.0.0-py2.4.egg/django/core/formfields.py", line 26, in __getitem__
    raise KeyError, "Field %s not found" % field_name

KeyError: 'Field company_id not found'

However as you can see from the below the SQL schema (PostgreSQL) contains the required information:

Table company:

CREATE TABLE company (
    id serial NOT NULL,
    name character varying(150) NOT NULL,
    type_id integer NOT NULL,
    status_id integer NOT NULL
);

Table addresses:

CREATE TABLE addresses (
    id serial NOT NULL,
    company_id integer NOT NULL,
    address_type_id integer NOT NULL,
    address1 character varying(150) NOT NULL,
    address2 character varying(150) NOT NULL,
    address3 character varying(150) DEFAULT ''::character varying NOT NULL,
    city_id integer NOT NULL,
    state_province_id integer NOT NULL,
    country_id integer NOT NULL,
    mail_code_id integer NOT NULL
);


ALTER TABLE public.addresses OWNER TO postgres;

--
-- Name: addresses_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres; Tablespace:
--

ALTER TABLE ONLY addresses
    ADD CONSTRAINT addresses_pkey PRIMARY KEY (id);


ALTER INDEX public.addresses_pkey OWNER TO postgres;

--
-- Name: addresses_address_type_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--

ALTER TABLE ONLY addresses
    ADD CONSTRAINT addresses_address_type_id_fkey FOREIGN KEY (address_type_id) REFERENCES address_type(id);


--
-- Name: addresses_city_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--

ALTER TABLE ONLY addresses
    ADD CONSTRAINT addresses_city_id_fkey FOREIGN KEY (city_id) REFERENCES cities(id);


--
-- Name: addresses_company_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--

ALTER TABLE ONLY addresses
    ADD CONSTRAINT addresses_company_id_fkey FOREIGN KEY (company_id) REFERENCES company(id);


--
-- Name: addresses_country_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--

ALTER TABLE ONLY addresses
    ADD CONSTRAINT addresses_country_id_fkey FOREIGN KEY (country_id) REFERENCES countries(id);


--
-- Name: addresses_mail_code_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--

ALTER TABLE ONLY addresses
    ADD CONSTRAINT addresses_mail_code_id_fkey FOREIGN KEY (mail_code_id) REFERENCES mailcodes(id);


--
-- Name: addresses_state_province_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--

ALTER TABLE ONLY addresses
    ADD CONSTRAINT addresses_state_province_id_fkey FOREIGN KEY (state_province_id) REFERENCES state_province(id);



comment:2 by oz.robharvey@…, 19 years ago

Severity: blockernormal

Hi JD,
Don't know if you are still working on this but the problem is you tried to use a foreign key with "_id" at the end of the field name. Django will try to append that string and apparently misinterpreted.
If you said:

class Address(meta.Model):
    company = ForeignKey(....)

you should have no problems.

BTW, when you do this, refer to the object that you want to look up by the method instead of the field name. i.e. if you wanted to add a repr() to your Address class, try this:

    def __repr__(self):
        return self.get_company().__repr__() + address1

I'm going to mark this back to normal because it appears not to block if you do things that way. But I don't know if the ordering is working right or not, so I can close.

comment:3 by asmodai@…, 19 years ago

milestone: Version 0.91Version 0.92

0.91 was released, this should move to 0.92 then.

comment:4 by jd@…, 19 years ago

I specific problem seems to be fixed in a later update. I am unsure of what actually fixed it though. It does not appear they were the same problem.

comment:5 by Adrian Holovaty, 19 years ago

Resolution: fixed
Status: newclosed

If it's fixed, I'll mark as closed.

comment:6 by Adrian Holovaty, 18 years ago

milestone: Version 0.92

Milestone Version 0.92 deleted

Note: See TracTickets for help on using tickets.
Back to Top