Repository: WeblateOrg/weblate

Issue:
REST API: Translation instances cannot be found under their URLs when inside a category
### Describe the issue

I'm getting 404s when trying to GET a translation instance which has a category.


### I already tried

- [X] I've read and searched [the documentation](https://docs.weblate.org/).
- [X] I've searched for similar issues in this repository.

### Steps to reproduce the behavior

1. Component's translation list endpoint: https://hosted.weblate.org/api/components/python-docs/latest%252Fbugs/translations/
2. The translation inside points to https://hosted.weblate.org/api/translations/python-docs/latest%252Fbugs/pl/ which gives 404.
3. The translation is present at the "old" non-category URL: https://hosted.weblate.org/api/translations/python-docs/bugs/pl/

### Expected behavior

_No response_

### Screenshots

_No response_

### Exception traceback

_No response_

### How do you run Weblate?

weblate.org service

### Weblate versions

[Weblate weblate-5.0-292-g754f16e286](https://weblate.org/) [Django 4.2.5](https://www.djangoproject.com/) [siphashc 2.1](https://github.com/WeblateOrg/siphashc) [translate-toolkit 3.10.1](https://toolkit.translatehouse.org/) [lxml 4.9.3](https://lxml.de/) [Pillow 10.0.0](https://python-pillow.org/) [nh3 0.2.14](https://pypi.org/project/Source%20Code/) [python-dateutil 2.8.2](https://github.com/dateutil/dateutil) [social-auth-core 4.4.2](https://github.com/python-social-auth/social-core) [social-auth-app-django 5.3.0](https://github.com/python-social-auth/social-app-django) [django-crispy-forms 2.0](https://github.com/django-crispy-forms/django-crispy-forms) [oauthlib 3.2.2](https://github.com/oauthlib/oauthlib) [django-compressor 4.4](https://django-compressor.readthedocs.io/en/latest/) [djangorestframework 3.14.0](https://www.django-rest-framework.org/) [django-filter 23.2](https://github.com/carltongibson/django-filter/tree/main) [django-appconf 1.0.5](https://django-appconf.readthedocs.io/) [user-agents 2.2.0](https://github.com/selwin/python-user-agents) [filelock 3.12.3](https://github.com/tox-dev/py-filelock) [rapidfuzz 3.2.0](https://github.com/maxbachmann/RapidFuzz) [openpyxl 3.1.2](https://openpyxl.readthedocs.io/) [celery 5.3.4](https://docs.celeryq.dev/) [django-celery-beat 2.5.0](https://github.com/celery/django-celery-beat) [kombu 5.3.2](https://kombu.readthedocs.io/) [translation-finder 2.15](https://weblate.org/) [weblate-language-data 2023.5](https://weblate.org/) [html2text 2020.1.16](https://github.com/Alir3z4/html2text/) [pycairo 1.24.0](https://pycairo.readthedocs.io/) [PyGObject 3.44.1](https://pygobject.readthedocs.io/) [diff-match-patch 20230430](https://pypi.org/project/Github/) [requests 2.31.0](https://requests.readthedocs.io/) [django-redis 5.3.0](https://github.com/jazzband/django-redis) [hiredis 2.2.3](https://github.com/redis/hiredis-py) [sentry-sdk 1.30.0](https://github.com/getsentry/sentry-python) [Cython 3.0.2](https://cython.org/) [misaka 2.1.1](https://github.com/FSX/misaka) [GitPython 3.1.34](https://github.com/gitpython-developers/GitPython) [borgbackup 1.2.6](https://borgbackup.readthedocs.io/) [pyparsing 3.1.1](https://github.com/pyparsing/pyparsing/) [ahocorasick_rs 0.16.0](https://github.com/G-Research/ahocorasick_rs) [python-redis-lock 4.0.0](https://github.com/ionelmc/python-redis-lock) [charset-normalizer 3.2.0](https://github.com/Ousret/charset_normalizer) [Python 3.9.2](https://www.python.org/) [Git 2.30.2](https://git-scm.com/) [psycopg2-binary 2.9.7](https://psycopg.org/) [phply 1.2.6](https://github.com/viraptor/phply) [ruamel.yaml 0.17.32](https://sourceforge.net/p/ruamel-yaml/code/ci/default/tree) [tesserocr 2.6.1](https://github.com/sirfz/tesserocr) [boto3 1.28.40](https://github.com/boto/boto3) [zeep 4.2.1](https://docs.python-zeep.org/) [aeidon 1.12](https://github.com/otsaloma/gaupol) [iniparse 0.5](https://github.com/candlepin/python-iniparse) [mysqlclient 2.2.0](https://pypi.org/project/Documentation/) [Mercurial 6.5.1](https://www.mercurial-scm.org/) [git-svn 2.30.2](https://git-scm.com/docs/git-svn) [git-review 2.3.1](https://pypi.org/project/git-review/) [PostgreSQL server 13.9](https://www.postgresql.org/)

### Weblate deploy checks

_No response_

### Additional context

_No response_


Discussion:

Resolving patch:
diff --git a/weblate/api/views.py b/weblate/api/views.py
--- a/weblate/api/views.py
+++ b/weblate/api/views.py
@@ -168,7 +168,7 @@ def get_object(self):
         lookup = {}
         category_path = ""
         for field in reversed(self.lookup_fields):
-            if field not in ("category__slug", "slug"):
+            if field not in ("component__slug", "slug"):
                 lookup[field] = self.kwargs[field]
             else:
                 category_prefix = field[:-4]
@@ -187,9 +187,7 @@ def get_object(self):
                         was_category = True
                 if not was_category:
                     # No category
-                    lookup[
-                        f"{category_prefix}{'__' if category_prefix else ''}category"
-                    ] = None
+                    lookup[f"{category_prefix}category"] = None
 
         # Lookup the object
         return get_object_or_404(queryset, **lookup)


Test patch:
diff --git a/weblate/api/tests.py b/weblate/api/tests.py
--- a/weblate/api/tests.py
+++ b/weblate/api/tests.py
@@ -3943,3 +3943,10 @@ def test_component(self):
         self.assertIn("category-test%252Ftest", response.data["url"])
         component = Component.objects.get(pk=self.component.pk)
         self.assertEqual(component.get_url_path(), ("test", "category-test", "test"))
+
+        # Verify that browsing translations works
+        response = self.do_request(response.data["url"] + "translations/")
+        self.assertEqual(response.data["count"], 4)
+
+        for translation in response.data["results"]:
+            self.do_request(translation["url"])