question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Right way to represent relationships links.related view?

See original GitHub issue

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:

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:closed
  • Created 5 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
Anton-Shutikcommented, Aug 14, 2018

@n2ygk yep, probably @sliverc will merge it soon.

0reactions
n2ygkcommented, Aug 10, 2018

@Anton-Shutik Maybe I should take a closer look at #451

Read more comments on GitHub >

github_iconTop Results From Across the Web

Guide to table relationships - Microsoft Support
Link tables in Access desktop databases by adding joins and creating relationships.
Read more >
Relationships - The Data Visualisation Catalogue
Relationships : Visualization methods that show relationships and connections between the data or show correlations between two or more variables.
Read more >
API design: Why you should use links, not keys, to represent ...
Representing relationships using links​​ The primary difference is that the relationships are expressed using links, rather than foreign key ...
Read more >
Chapter 4: Interaction with Linked Data - EUCLID
Linked Data visualization techniques aim to provide graphical ... Containment can be used to represent hierarchical relationships between nodes.
Read more >
The power of relationships in data | All Things Distributed
Both graphs consist of nodes (sometimes called "vertices") and directed edges (sometimes called "links"). Both graphs allow properties ( ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found