Skip to content
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

Improve /api/jobs handling of models with many to many relationships #23

Open
ojongerius opened this issue Oct 11, 2017 · 0 comments
Open

Comments

@ojongerius
Copy link
Member

ojongerius commented Oct 11, 2017

Current implementation was a bit of a cop out: listing jobs will return the id instead of values for objects with many to many relationships. For example, look at the values for sites and labels:

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

{
    "count": 4,
    "next": null,
    "previous": null,
    "results": [
        {
            "id": 1,
            "title": "Reading Support Person",
            "text": "Volunteer will work either one-on-one or in a small group - no more than 4 - to help students increase reading mileage an d comprehension. Students often need help in developing vocabulary and reading strategies for unknown words. We have a hig h number of English language learners (ELL) and they benefit from having support in this area",
            "labels": [
                5
            ],
            "organisation": {
                "id": 1,
                "name": "Volunteer Wellington",
                "region": "wellington",
                "city": "wellington",
                "url": "https://www.volunteerwellington.org.nz/",
                "created_at": "2017-10-08T22:36:24.078728Z",
                "updated_at": "2017-10-08T22:36:24.103673Z"
            },
            "sites": [
                1
            ],
            "country": "new zealand",
            "region": "wellington",
            "created_at": "2017-10-08T22:36:23.965767Z",
            "updated_at": "2017-10-08T22:36:24.030438Z",
            "url": "https://seekvolunteer.co.nz/volunteering/49932/reading-support-person",
            "seen": 0
        },
        {
            "id": 2,
            "title": "Gardener",
            "text": "Basic gardening to ensure vegetable gardens remain weeded fertilised and watered in the spring and summer growing seasons . Every August we have a team of volunteers come in and prepare the gardens for spring but the gardens need ongoing mainte nance to ensure they are as productive as possible. Ideal role for a Karori local to come once or twice a week to maintain  and water garden and ensure planting happens at right times for Karori climate but anyone keen is welcome",
            "labels": [
                4
            ],
            "organisation": {
                "id": 1,
                "name": "Volunteer Wellington",
                "region": "wellington",
                "city": "wellington",
                "url": "https://www.volunteerwellington.org.nz/",
                "created_at": "2017-10-08T22:36:24.078728Z",
                "updated_at": "2017-10-08T22:36:24.103673Z"
            },
            "sites": [
                1
            ],
            "country": "new zealand",
            "region": "wellington",
            "created_at": "2017-10-08T22:36:23.965767Z",
            "updated_at": "2017-10-08T22:36:24.030438Z",
            "url": "https://seekvolunteer.co.nz/volunteering/49914/gardener",
            "seen": 0
        },
        {
            "id": 3,
            "title": "Companion Volunteer",
            "text": "Provide safe companionship and support to the person with dementia for 1-2 hours weekly or fortnightly. This may include (but is not limited to) tasks such as visiting a cafe or library, going for a walk, making a cup of tea, reading the newsp aper together, watching sport etc. Police check required. TRAINING: Initial induction of two 2-hour sessions and ongoing o ptional training throughout the year. How to Apply",
            "labels": [
                5
            ],
            "organisation": {
                "id": 2,
                "name": "Dementia Canterbury",
                "region": "canterbury",
                "city": "christchurch",
                "url": "https://www.dementiacanterbury.org.nz/",
                "created_at": "2017-10-08T22:36:24.078728Z",
                "updated_at": "2017-10-08T22:36:24.103673Z"
            },
            "sites": [
                2
            ],
            "country": "new zealand",
            "region": "wellington",
            "created_at": "2017-10-08T22:36:23.965767Z",
            "updated_at": "2017-10-08T22:36:24.030438Z",
            "url": "https://dogoodjobs.co.nz/jobs/companion-volunteer",
            "seen": 0
        },
        {
            "id": 4,
            "title": "Nappy changer",
            "text": "This is a great opportunity for young and old",
            "labels": [
                5
            ],
            "organisation": {
                "id": 1,
                "name": "Volunteer Wellington",
                "region": "wellington",
                "city": "wellington",
                "url": "https://www.volunteerwellington.org.nz/",
                "created_at": "2017-10-08T22:36:24.078728Z",
                "updated_at": "2017-10-08T22:36:24.103673Z"
            },
            "sites": [
                2
            ],
            "country": "new zealand",
            "region": "otago",
            "created_at": "2017-10-08T22:36:23.965767Z",
            "updated_at": "2017-10-08T22:36:24.030438Z",
            "url": "https://dogoodjobs.co.nz/nappy-changer",
            "seen": 0
        }
    ]
}

This was an easy way of getting the API working with many2many relationships for both getting and creating nested structures like the Job model.

DoD:

  • Getting items from the jobs endpoints will expend nested models Labels and Sites will expand them, when updating job.
  • Creating or updating Jobs allows either passing in the id, or passing in the nested structure.

See:

@ojongerius ojongerius changed the title Improve nested structures in API Improve /api/jobs handling of models with many to many relationships Oct 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant