Django-Rest-Framework更新外键BY Id [英] Django-Rest-Framework updating a foreign key BY Id
问题描述
我正在使用django-rest-framework来构建后端。我的列表运行正常,但(使用django-rest-framework管理屏幕)我不能通过使用外键对象的Id字段创建一个对象。我希望我的配置不正确,但我可以编写一些代码,如果我必须:)我正在从.NET和Java背景学习django / python,可能已经成为这个新的堆栈的一个触摸。
I am using django-rest-framework to build out the back end. I have the list running fine, but (using the django-rest-framework admin screen) I cannot create an object by just using the Id fields of the foreign key objects. I hope I have this configured incorrectly, but I am open to writing some code if i have to :) I am learning django/python from a .NET and Java background and may have become a touch spoiled by this new stack.
编辑:我试图不使用两个不同的模型类 - 我不应该是正确的?
提前感谢
从Chrome - 请求的关键位置
From Chrome - the key bits of the request
Request URL:http://127.0.0.1:8000/rest/favorite_industries/
Request Method:POST
_content_type:application/json
_content:{
"user_id": 804 ,"industry_id": 20 }
回复
HTTP 400 BAD REQUEST
Vary: Accept
Content-Type: text/html; charset=utf-8
Allow: GET, POST, HEAD, OPTIONS
{
"user": [
"This field is required."
]
}
以下是django的关键课程:
Ugh. Here are the key classes from django:
class FavoriteIndustry(models.Model):
id = models.AutoField(primary_key=True)
user = models.ForeignKey(User, related_name='favorite_industries')
industry = models.ForeignKey(Industry)
class Meta:
db_table = 'favorites_mas_industry'
class FavoriteIndustrySerializer(WithPkMixin, serializers.HyperlinkedModelSerializer):
class Meta:
model = myModels.FavoriteIndustry
fields = (
'id'
, 'user'
, 'industry'
)
编辑添加视图集
class FavoriteIndustriesViewSet(viewsets.ModelViewSet):
#mixins.CreateModelMixin, viewsets.GenericViewSet):
paginate_by = 1
queryset = myModels\
.FavoriteIndustry\
.objects\
.select_related()
print 'SQL::FavoriteIndustriesViewSet: ' + str(queryset.query)
serializer_class = mySerializers.FavoriteIndustrySerializer
获取/列表功能生成不错的JSON:
The get/list functionality generates decent JSON:
{count :2,下一个:
http://blah.com/rest/favorite_industries /?page = 2& format = json ,
previous:null,results:[{id:1,user:
http://blah.com/rest/users/804/ ,行业:{industry_id:
2,industry_name:Consumer Discretionary,parent_industry_name:
Consumer Discretionary,category_name:Industries}}]}
{"count": 2, "next": "http://blah.com/rest/favorite_industries/?page=2&format=json", "previous": null, "results": [{"id": 1, "user": "http://blah.com/rest/users/804/", "industry": {"industry_id": 2, "industry_name": "Consumer Discretionary", "parent_industry_name": "Consumer Discretionary", "category_name": "Industries"}}]}
推荐答案
我已经创建了一个简化的应用程序模型。
I have created a simplified mock up of your application.
models.py :
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)
views.py:
from rest_framework import viewsets
from models import FavoriteIndustry
from serializers import FavoriteIndustrySerializer
class FavoriteIndustriesViewSet(viewsets.ModelViewSet):
queryset = FavoriteIndustry.objects.all()
serializer_class = FavoriteIndustrySerializer
serializers.py:
from rest_framework import serializers
from models import FavoriteIndustry, Industry
class FavoriteIndustrySerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = FavoriteIndustry
fields = ('id', 'user', 'industry')
urls.py:
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<pk>[0-9]+)/$', favorite_industries_list, name='user-detail'),
url(r'^industries/(?P<pk>[0-9]+)/$', favorite_industries_list, name='industry-detail'),
)
这里有几个测试:
>>>
>>> import json
>>> from django.test import Client
>>> from core.models import Industry
>>>
>>> industry = Industry(name='candy')
>>> industry.save()
>>>
>>> c = Client()
>>>
>>> response = c.get('http://localhost:8000/favorite_industries/')
>>> response.content
'[]'
>>>
>>> data = {
... 'user': 'http://localhost:8000/users/1/',
... 'industry': 'http://localhost:8000/industries/1/'
... }
>>>
>>> response = c.post('http://localhost:8000/favorite_industries/', json.dumps(data), 'application/json')
>>> response.content
'{"id": 1, "user": "http://testserver/users/1/", "industry": "http://testserver/industries/1/"}'
>>>
>>> response = c.get('http://localhost:8000/favorite_industries/')
>>> response.content
'[{"id": 1, "user": "http://testserver/users/1/", "industry": "http://testserver/industries/1/"}]'
>>>
Django REST框架期待用户
和行业
字段为URL而不是id,因为您使用 HyperlinkedModelSerializer
。
Django REST Framework expects user
and industry
fields as URLs rather than ids since you are using HyperlinkedModelSerializer
.
如果您需要使用对象ids而不是URL,请使用 ModelSerializer
而不是 HyperlinkedModelSerializer
并将ids传递给用户
和行业
:
In case you need to use object ids instead of URLs, use ModelSerializer
instead of HyperlinkedModelSerializer
and pass ids to user
and industry
:
serializers.py:
from rest_framework import serializers
from models import FavoriteIndustry, Industry
class FavoriteIndustrySerializer(serializers.ModelSerializer):
class Meta:
model = FavoriteIndustry
fields = ('id', 'user', 'industry')
测试:
>>>
>>> import json
>>> from django.test import Client
>>> from core.models import Industry
>>>
>>> #industry = Industry(name='candy')
>>> #industry.save()
>>>
>>> c = Client()
>>>
>>> response = c.get('http://localhost:8000/favorite_industries/')
>>> response.content
'[{"id": 1, "user": 1, "industry": 1}, {"id": 2, "user": 1, "industry": 1}]'
>>>
>>> data = {
... 'user': 1,
... 'industry': 1
... }
>>>
>>> response = c.post('http://localhost:8000/favorite_industries/', json.dumps(data), 'application/json')
>>> response.content
'{"id": 3, "user": 1, "industry": 1}'
>>>
>>> response = c.get('http://localhost:8000/favorite_industries/')
>>> response.content
'[{"id": 1, "user": 1, "industry": 1}, {"id": 2, "user": 1, "industry": 1}, {"id": 3, "user": 1, "industry": 1}]'
>>>
这篇关于Django-Rest-Framework更新外键BY Id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!