Multiple databases - unmanaged models - get_absolute_url()

59 views
Skip to first unread message

David Chandek-Stark

unread,
Apr 29, 2011, 1:18:53 PM4/29/11
to django...@googlegroups.com
No doubt this is an edge case, but here goes ...

With Django 1.3/Python 2.4 I have an app consisting of unmanaged models in alternate (non-default) database.  Each model has defined get_absolute_url() simply as a fully-qualified (i.e.,external) URL string in which the instance id is substituted.   Everything seems to work fine, except the "View on site" link on the object change pages, which raises a DatabaseError (no such table exists) exception.  The link resolves to the view django.contrib.contenttypes.views.shortcut, which gets the ContentType instance for the object, then re-retrieves the original object (!?) using the ContentType.get_object_for_this_type() method.  Now, I think the problem is that this method uses the database for the contenttypes app instead of the alternate database (see http://code.djangoproject.com/browser/django/tags/releases/1.3/django/contrib/contenttypes/models.py#L102) because it explicitly calls using() with the model state of the ContentType object.

Am I reading the Django code right, or am I doing something wrong?

Thanks,
David

Ramiro Morales

unread,
Apr 29, 2011, 2:54:11 PM4/29/11
to django...@googlegroups.com
On Fri, Apr 29, 2011 at 2:18 PM, David Chandek-Stark
<dchand...@gmail.com> wrote:

> [...]


>  Now, I think the problem
> is that this method uses the database for the contenttypes app instead of
> the alternate database
> (see http://code.djangoproject.com/browser/django/tags/releases/1.3/django/contrib/contenttypes/models.py#L102)
> because it explicitly calls using() with the model state of the ContentType
> object.
> Am I reading the Django code right, or am I doing something wrong?

Coincidentally, someone else has atached a patch for ticket [1]15610 today.

Any help with tests for the proposed fix is welcome.

--
Ramiro Morales

1. http://code.djangoproject.com/ticket/15610

David Chandek-Stark

unread,
Apr 29, 2011, 4:32:18 PM4/29/11
to django...@googlegroups.com
OK, that's good.  However, I don't really understand why the admin site employs such an extremely convoluted method here.  If you follow the logic from the change form, here's what happens:

if has_absolute_url (i.e., the object model has a "get_absolute_url" attribute) ...
  -> display "view on site" link, which goes to ...
  -> view that looks up the content type object, then...
  -> gets the object of that type having the id passed in, then ...
  -> calls get_absolute_url() on the object, and ...
  -> redirects to that url

How is that ultimately different from:

if has_absolute_url 
  -> display "view on site" link with href="{{ obj.get_absolute_url }}"

(except that it appears the admin change form view doesn't pass the obj to the template context?!) ?

--David

David Chandek-Stark

unread,
Apr 29, 2011, 6:57:26 PM4/29/11
to django...@googlegroups.com
I submitted a patch to the admin change form template -- http://code.djangoproject.com/ticket/15931.

Meanwhile, you can temporarily get around the problem on a per-application basis by adding the attached file to the application package directory at templates/admin/<app_label>/change_form.html.

--D
change_form.html
Reply all
Reply to author
Forward
0 new messages