-
Notifications
You must be signed in to change notification settings - Fork 301
Right way to represent relationships links.related view? #426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Here's my workaround but I suspect I'm just missing the obvious right way!
url(r'^v1/courses/(?P<pk>[^/.]+)/relationships/(?P<related_field>\w+)',
views.CourseRelationshipView.as_view(),
name='course-relationships'),
url(r'^v1/courses/(?P<pk>[^/.]+)/course_terms/',
views.CourseTermViewSet.as_view({'get': 'list'}),
name='course-course_terms'),
class CourseTermViewSet(CourseBaseViewSet):
# API endpoint that allows CourseTerm to be viewed or edited.
# if lookup_field (pk) is passed in to the view, then the query is filtered for matching foreign keys
# TODO: there must be a correct way to do this as the RelationshipView does the right thing.
queryset = CourseTerm.objects.all()
serializer_class = CourseTermSerializer
def list(self, request, *args, **kwargs):
if __class__.lookup_field in kwargs:
print("FK {}".format(kwargs[__class__.lookup_field]))
self.queryset = self.get_queryset().filter(course_id=kwargs[__class__.lookup_field])
return super(CourseTermViewSet, self).list(request, *args, **kwargs) |
I haven't really used Potentially it is a bug. If you can reproduce it in the example app best write a test case and open a PR. Potentially a fix might be obvious then and can be added as well or someone else can have a look. |
Yes, RelationShipView works fine in that it generates the appropriate
related links.self and links.related URLs. There's just no documentation
for how to correctly implement the view that corresponds to links.related
and the example is incorrect in that it doesn't filter the child model by
the parent's pk. I suspect it may not have been implemented. I'm thinking
of reposting to stackoverflow as there's a larger audience there and maybe
someone else has solved this already.
Once I get a solution I'll submit a PR to fix the example and add more
complete test cases.
…On Fri, Apr 27, 2018 at 11:07 AM, Oliver Sauder ***@***.***> wrote:
I haven't really used RelationshipVIew. But I assume you have followed
the documentation on http://django-rest-framework-
json-api.readthedocs.io/en/stable/usage.html?highlight=RelationshipView#
relationshipview
Potentially it is a bug. If you can reproduce it in the example app best
write a test case and open a PR. Potentially a fix might be obvious then
and can be added as well or someone else can have a look.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#426 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AEJ5d5yc3B3nlbmveX9H6xRDEdAo__rWks5tszQqgaJpZM4Tn0RK>
.
|
@n2ygk I think you can do like it was done in example: Configure url with parameter named
Then in the view you can check
Thus, it will return only comments related to given entry. |
@Anton-Shutik Thanks, yes, here's what I've ended up with that works basically the same way, just overriding router = rest_framework.routers.DefaultRouter()
# ...
router.register(r'course_terms', views.CourseTermViewSet)
urlpatterns = [
# ...
url(r'^v1/', include(router.urls)),
url(r'^v1/course_terms/(?P<pk>[^/.]+)/relationships/(?P<related_field>\w+)',
views.CourseTermRelationshipView.as_view(),
name='course_term-relationships'),
url(r'^v1/course_terms/(?P<pk>[^/.]+)/course/',
views.CourseViewSet.as_view({'get': 'list'}),
name='course_terms-course'),
]
class CourseTermViewSet(CourseBaseViewSet):
queryset = CourseTerm.objects.all()
serializer_class = CourseTermSerializer
def list(self, request, *args, **kwargs):
if __class__.lookup_field in kwargs:
self.queryset = self.get_queryset().filter(course=kwargs[__class__.lookup_field])
return super(CourseTermViewSet, self).list(request, *args, **kwargs) What would be really nice to have is a |
@Anton-Shutik Maybe I should take a closer look at #451 |
I'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:
A get of a course looks like this:
GET 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 withfk=ec008c20-79a1-4ca7-931a-019d62c219c9
because the foreign key is not being applied. (You can see where I tested and withqueryset = 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.
The text was updated successfully, but these errors were encountered: