Tuesday, June 29, 2010

"View on Site" for instance with Many-To-Many relation to the Site model

Hello,

I'm using a model with a many to many relationship to Site. I noticed
that the redirect from the admin site (the link "View on Site") is to
a domain that is not associated with the current SITE_ID. in other
words I have two sites, "devel.domain.com" and "www.domain.com." When
I select "View on Site" when using the admin on the www.domain.com
site, it sends me to the resource on the devel.domain.com instead.

Inspecting the admin code, I see the url conf for redirects on line
227 in
django/contrib/admin/sites.py

Tracing the code backwards I eventually get to
django.contrib.contenttypes.views.shortcut, the view that performs the
redirect. I see in that view, the model instance we are trying to
"View on Site" is inspected to determine if it has a relation (Many-to-
Many or Many-To-One) to the Site model.

starting on line 32 in django/contrib/contenttypes/views.py we see
this chunk of code dealing with the Many To Many:
*snip*

# First, look for an many-to-many relationship to Site.
for field in opts.many_to_many:
if field.rel.to is Site:
try:
# Caveat: In the case of multiple related Sites, this
just
# selects the *first* one, which is arbitrary.
object_domain = getattr(obj, field.name).all()
[0].domain
except IndexError:
pass
if object_domain is not None:
break

*endsnip*

What this code is doing is attempting to find a domain to concatenate
with the result of `get_absolute_url()`

My question is, why do we a arbitrarily select the first Site instance
in the many to many relationship? Wouldn't it make sense to pick the
Site instance associated with the running SITE_ID? The reason I ask
this is that it is confusing for the admin to redirect away from the
running site, to another site.

I believe this to be a question of correctness. Rather than choosing,
arbitrarily, a random site to redirect to, why not redirect, again
arbitrarily, to the current site? Anyways, I would appreciate any
feedback on this, it has come up and I am at a loss to explain this
behavior in the admin. Am I missing something?

Thanks much,
Sam

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment