0

I have a project with two apps:

  • accounts,
  • classes.

In accounts app I am using Django's Group model (for account permissions) and in classes app I have another one model with the name Group (but it should be interpreted as a group of students).

My url.py looks like:

(...)
from rest_framework import routers
from .accounts import views as accounts_views
from .classes import views as classes_views

router = routers.DefaultRouter()
(...)
router.register(r"groups", accounts_views.GroupViewSet)
router.register(r"classes/groups", classes_views.GroupViewSet)
(...)

But it is not working as expected. /api page looks as follows:

HTTP 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    (...)
    "groups": "http://127.0.0.1:8000/api/classes/groups/",
    "classes/groups": "http://127.0.0.1:8000/api/classes/groups/",
    (...)
}

So, both targets the same address what is not what I want. Admin page works well (I can edit account's Group and classes's Group).

Any suggestions?

Edit:

Changing one of url.py's line as below (as suggested in one of answers below):

router.register(r"classes_groups", classes_views.GroupViewSet)

changes /api page to:

HTTP 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    (...)
    "groups": "http://127.0.0.1:8000/api/classes_groups/",
    "classes/groups": "http://127.0.0.1:8000/api/classes_groups/",
    (...)
}

So @argaen solution generally is not a solution for this case.

Tomasz Dzieniak
  • 2,765
  • 3
  • 24
  • 42
  • I think, naming models with the same name is not the best idea in any case. For me it's hard to predict, what else problems could happen, but semantically it's wrong way to name objects. – Oleksii M Mar 14 '15 at 17:39
  • I agree to you. Whenever it is possible to avoid name clashes I do this (i. e. by using synonyms). But in this case using other name for Classes app Group model is totally nonintuitive. – Tomasz Dzieniak Mar 14 '15 at 18:21
  • This is an alternative but I'd prefer to not use such names of models (favor a short single-word names of models whenever possible). However, I'd like to now how to handle issues like described above. :) – Tomasz Dzieniak Mar 14 '15 at 18:29
  • OK, I understood your point, it has something – Oleksii M Mar 14 '15 at 18:53

2 Answers2

1

The url's name defaults to the lowercased model name, which in this case is 'group' for both viewsets. Thus, reverse cannot tell the difference and returns the first match for both.

I believe passing an explicit basename to at least one of the urls should fix it:

router = routers.DefaultRouter()
(...)
router.register(r"groups", accounts_views.GroupViewSet)
router.register(r"classes/groups", classes_views.GroupViewSet, "classes_group")
(...)
knbk
  • 52,111
  • 9
  • 124
  • 122
0

You can't add paths with slash in rest_framework routers. The format is this. Check also this question asking the same.

You can either use the solution given in the linked question or set the url like that:

router.register(r"classes_groups", classes_views.GroupViewSet)
Community
  • 1
  • 1
argaen
  • 4,145
  • 24
  • 28
  • Rather asking "almost" the same. Of course it is possible to use slashes - I even do this in the same project (but with no name clushes) and works fine. – Tomasz Dzieniak Mar 14 '15 at 17:44
  • And you got it working by calling the `router.register` with slashes in the url? – argaen Mar 14 '15 at 20:34
  • If you mean solution about which I asked above - no, I have not got it working, yet. If you mean generally (with no name clashes) - yes, using slashes in urls passed to DefaultRouter - it is working great. – Tomasz Dzieniak Mar 15 '15 at 00:00