Right way to represent relationships links.related view?
See original GitHub issueI’m trying to correctly return a links.related view (not links.self) and haven’t been able to find a good example. In looking at the example app it appears to have the same problem. Here’s a fragment of my urlconf and view definitions:
router = routers.DefaultRouter()
router.register(r'courses', views.CourseViewSet)
router.register(r'course_terms', views.CourseTermViewSet)
urlpatterns = [
url(r'^v1/', include(router.urls)),
# http://127.0.0.1:8000/v1/courses/f009e671-b615-42c7-b35f-8500d7ef5d24/relationships/course_terms
url(r'^v1/courses/(?P<pk>[^/.]+)/relationships/(?P<related_field>\w+)',
views.CourseRelationshipView.as_view(),
name='course-relationships'),
# http://127.0.0.1:8000/v1/courses/f009e671-b615-42c7-b35f-8500d7ef5d24/course_terms/
url(r'^v1/courses/(?P<fk>[^/.]+)/course_terms/', # need fk (course_id) not pk (id)
views.CourseTermViewSet.as_view({'get': 'list'}),
name='course-course_terms'),
]
class CourseBaseViewSet(AuthnAuthzMixIn, SortMixin, FilterMixin, viewsets.ModelViewSet):
pass
class CourseViewSet(CourseBaseViewSet):
# API endpoint that allows course to be viewed or edited.
queryset = Course.objects.all()
serializer_class = CourseSerializer
class CourseTermViewSet(CourseBaseViewSet):
# API endpoint that allows CourseTerm to be viewed or edited.
queryset = CourseTerm.objects.all()
serializer_class = CourseTermSerializer
# queryset = queryset.filter(course_id='ec008c20-79a1-4ca7-931a-019d62c219c9')
class CourseRelationshipView(AuthnAuthzMixIn, RelationshipView):
queryset = Course.objects
self_link_view_name = 'course-relationships'
A get of a course looks like this:
GET http://127.0.0.1:8000/v1/courses/ec008c20-79a1-4ca7-931a-019d62c219c9/
{
"data": {
"type": "courses",
"id": "ec008c20-79a1-4ca7-931a-019d62c219c9",
"attributes": {
"school_bulletin_prefix_code": "XCEFK9",
"suffix_two": "00",
"subject_area_code": "PSYB",
"course_number": "00241",
"course_identifier": "PSYC1138X",
"course_name": "SOCIAL PSYCHOLOGY-LEC",
"course_description": "SOCIAL PSYCHOLOGY-LEC",
"effective_start_date": null,
"effective_end_date": null,
"last_mod_user_name": "loader",
"last_mod_date": "2018-03-11"
},
"relationships": {
"course_terms": {
"data": [
{
"type": "course_terms",
"id": "3ce01e27-9a68-4970-b48a-e6a83166ca41"
}
],
"links": {
"self": "http://127.0.0.1:8000/v1/courses/ec008c20-79a1-4ca7-931a-019d62c219c9/relationships/course_terms",
"related": "http://127.0.0.1:8000/v1/courses/ec008c20-79a1-4ca7-931a-019d62c219c9/course_terms/"
},
"meta": {
"count": 1
}
}
},
"links": {
"self": "http://127.0.0.1:8000/v1/courses/ec008c20-79a1-4ca7-931a-019d62c219c9/"
}
}
}
But GET http://127.0.0.1:8000/v1/courses/ec008c20-79a1-4ca7-931a-019d62c219c9/course_terms/
returns all the course_terms, not just those with fk=ec008c20-79a1-4ca7-931a-019d62c219c9
because the foreign key is not being applied. (You can see where I tested and with queryset = queryset.filter(course_id='ec008c20-79a1-4ca7-931a-019d62c219c9')
) so I’m wondering what the right way to do this is.
Am I missing something? I’ve reproduced this with the example app and it also does it “wrong”. I think I need to extend the view
function to check for some kwargs (e.g. fk) and filter the manager.
Thanks in advance for any help.
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (7 by maintainers)
Top GitHub Comments
@n2ygk yep, probably @sliverc will merge it soon.
@Anton-Shutik Maybe I should take a closer look at #451