2016-06-14 2 views
-2

내가 가진 400을 반환 : OneToOneFields; POST 요청은 모두 지금

class Thing(BaseModel): 
    #doesn't matter 

class OtherThing(BaseModel): 
    thing = models.OneToOneField(Thing, 
    related_name = 'other_thing', 
    null   = True, 
    blank  = True, 
) 

지금 내 장고 백엔드는 늘 other_thing이 페이로드에 널 (null)로 설정되어있는 동안 나를 일 모델을 지속 할 수 있습니다. 개념적으로 나에게 의미가 없지만 1-2-1의 첫 번째 부분을 저장할 수 없다면 두 번째를 어떻게 저장할 수 있습니까?

api/물건에 대한 게시물 요청은 400을 반환합니다. { "other_thing": [ ".이 필드는 null이 될 수 없습니다"]}

직렬 변환기 :

class ThingSerializer(serializers.ModelSerializer): 

    class Meta: 
     model = Thing 
     fields = ('id', 'name', 'other_thing') 

class OtherThingSerializer(serializers.ModelSerializer): 

    class Meta: 
     model = OtherThing 
     fields = ('id', 'name', 'thing') 
+1

API 프레임 워크를 사용하고 있습니까? 귀하의 API 코드는 어떻게 생겼습니까? 정보가 충분하지 않습니다. –

+0

정말 아무 것도 없다. 저장시 full_clean을 호출하게하고, __str__ 메서드를 제공한다. 그것은 ForeignKey 일 때 일하고 있었고 OneToOneField로 변환 될 때 작업을 중단했기 때문에 1-2-1에 내재 된 것이 있으면 놓칠 수 있습니다. –

+1

시리얼 편집기를 보여줍니다 – hsfzxjy

답변

1

그래서 내가 그것을 알아 냈어, 정말 같은 read_only_field으로 'other_thing'을 설정하는 데 필요한;

class ThingSerializer(serializers.ModelSerializer): 

class Meta: 
    model = Thing 
    fields = ('id', 'name', 'other_thing') 
    read_only_field = ('other_thing',) 

은이 게시물에 올 때 페이로드에 기대에서 장고를 유지하지만 것 요청했을 때 여전히를 보내됩니다.

관련 문제