29 | | # Next, look for an many-to-many relationship to sites |
30 | | if hasattr(obj, 'get_site_list'): |
31 | | site_list = obj.get_site_list() |
32 | | if site_list: |
33 | | object_domain = site_list[0].domain |
| 29 | # First, look for an many-to-many relationship to sites |
| 30 | for field in obj._meta.many_to_many: |
| 31 | if field.rel.to is Site: |
| 32 | try: |
| 33 | object_domain = getattr(obj, field.name).all()[0].domain |
| 34 | except Site.DoesNotExist: |
| 35 | pass |
| 36 | break |
| 37 | else: |
| 38 | # Next, look for a many-to-one relationship to sites |
| 39 | for field in obj._meta.fields: |
| 40 | if field.rel and field.rel.to is Site: |
| 41 | try: |
| 42 | object_domain = getattr(obj, field.name).domain |
| 43 | except Site.DoesNotExist: |
| 44 | pass |
| 45 | break |
| 46 | else: |
| 47 | # Then, fall back to the current site (if possible) |
| 48 | try: |
| 49 | object_domain = Site.objects.get_current().domain |
| 50 | except Site.DoesNotExist: |
| 51 | # Finally, give up and use a URL without the domain name |
| 52 | return http.HttpResponseRedirect(obj.get_absolute_url()) |
35 | | # Next, look for a many-to-one relationship to sites |
36 | | elif hasattr(obj, 'get_site'): |
37 | | try: |
38 | | object_domain = obj.get_site().domain |
39 | | except Site.DoesNotExist: |
40 | | pass |
41 | | |
42 | | # Then, fall back to the current site (if possible) |
43 | | else: |
44 | | try: |
45 | | object_domain = Site.objects.get_current().domain |
46 | | except Site.DoesNotExist: |
47 | | # Finally, give up and use a URL without the domain name |
48 | | return http.HttpResponseRedirect(obj.get_absolute_url()) |