Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have created a simplified mock up of your application.</p> <p><strong>models.py:</strong></p> <pre><code>from django.db import models from django.contrib.auth.models import User class Industry(models.Model): name = models.CharField(max_length=128) class FavoriteIndustry(models.Model): user = models.ForeignKey(User, related_name='favorite_industries') industry = models.ForeignKey(Industry) </code></pre> <p><strong>views.py:</strong></p> <pre><code>from rest_framework import viewsets from models import FavoriteIndustry from serializers import FavoriteIndustrySerializer class FavoriteIndustriesViewSet(viewsets.ModelViewSet): queryset = FavoriteIndustry.objects.all() serializer_class = FavoriteIndustrySerializer </code></pre> <p><strong>serializers.py:</strong></p> <pre><code>from rest_framework import serializers from models import FavoriteIndustry, Industry class FavoriteIndustrySerializer(serializers.HyperlinkedModelSerializer): class Meta: model = FavoriteIndustry fields = ('id', 'user', 'industry') </code></pre> <p><strong>urls.py:</strong></p> <pre><code>from django.conf.urls import patterns, include, url from core.api import FavoriteIndustriesViewSet favorite_industries_list = FavoriteIndustriesViewSet.as_view({ 'get': 'list', 'post': 'create' }) urlpatterns = patterns('', url(r'^favorite_industries/$', favorite_industries_list, name='favorite-industries-list'), url(r'^users/(?P&lt;pk&gt;[0-9]+)/$', favorite_industries_list, name='user-detail'), url(r'^industries/(?P&lt;pk&gt;[0-9]+)/$', favorite_industries_list, name='industry-detail'), ) </code></pre> <p>And here are a few tests:</p> <pre><code>&gt;&gt;&gt; &gt;&gt;&gt; import json &gt;&gt;&gt; from django.test import Client &gt;&gt;&gt; from core.models import Industry &gt;&gt;&gt; &gt;&gt;&gt; industry = Industry(name='candy') &gt;&gt;&gt; industry.save() &gt;&gt;&gt; &gt;&gt;&gt; c = Client() &gt;&gt;&gt; &gt;&gt;&gt; response = c.get('http://localhost:8000/favorite_industries/') &gt;&gt;&gt; response.content '[]' &gt;&gt;&gt; &gt;&gt;&gt; data = { ... 'user': 'http://localhost:8000/users/1/', ... 'industry': 'http://localhost:8000/industries/1/' ... } &gt;&gt;&gt; &gt;&gt;&gt; response = c.post('http://localhost:8000/favorite_industries/', json.dumps(data), 'application/json') &gt;&gt;&gt; response.content '{"id": 1, "user": "http://testserver/users/1/", "industry": "http://testserver/industries/1/"}' &gt;&gt;&gt; &gt;&gt;&gt; response = c.get('http://localhost:8000/favorite_industries/') &gt;&gt;&gt; response.content '[{"id": 1, "user": "http://testserver/users/1/", "industry": "http://testserver/industries/1/"}]' &gt;&gt;&gt; </code></pre> <p>Django REST Framework expects <code>user</code> and <code>industry</code> fields as URLs rather than ids since you are using <code>HyperlinkedModelSerializer</code>.</p> <hr> <h1>Using IDs</h1> <p>In case you need to use object ids instead of URLs, use <code>ModelSerializer</code> instead of <code>HyperlinkedModelSerializer</code> and pass ids to <code>user</code> and <code>industry</code>:</p> <p><strong>serializers.py:</strong></p> <pre><code>from rest_framework import serializers from models import FavoriteIndustry, Industry class FavoriteIndustrySerializer(serializers.ModelSerializer): class Meta: model = FavoriteIndustry fields = ('id', 'user', 'industry') </code></pre> <p>And tests:</p> <pre><code>&gt;&gt;&gt; &gt;&gt;&gt; import json &gt;&gt;&gt; from django.test import Client &gt;&gt;&gt; from core.models import Industry &gt;&gt;&gt; &gt;&gt;&gt; #industry = Industry(name='candy') &gt;&gt;&gt; #industry.save() &gt;&gt;&gt; &gt;&gt;&gt; c = Client() &gt;&gt;&gt; &gt;&gt;&gt; response = c.get('http://localhost:8000/favorite_industries/') &gt;&gt;&gt; response.content '[{"id": 1, "user": 1, "industry": 1}, {"id": 2, "user": 1, "industry": 1}]' &gt;&gt;&gt; &gt;&gt;&gt; data = { ... 'user': 1, ... 'industry': 1 ... } &gt;&gt;&gt; &gt;&gt;&gt; response = c.post('http://localhost:8000/favorite_industries/', json.dumps(data), 'application/json') &gt;&gt;&gt; response.content '{"id": 3, "user": 1, "industry": 1}' &gt;&gt;&gt; &gt;&gt;&gt; response = c.get('http://localhost:8000/favorite_industries/') &gt;&gt;&gt; response.content '[{"id": 1, "user": 1, "industry": 1}, {"id": 2, "user": 1, "industry": 1}, {"id": 3, "user": 1, "industry": 1}]' &gt;&gt;&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload