2014-02-26 21:08:26 +04:00
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
|
2014-04-13 02:05:57 +04:00
|
|
|
from django.conf.urls import patterns, url
|
2014-08-29 15:35:53 +04:00
|
|
|
from rest_framework import serializers, generics
|
2014-02-26 21:08:26 +04:00
|
|
|
from rest_framework.test import APITestCase
|
2014-03-02 15:40:30 +04:00
|
|
|
from tests.models import NullableForeignKeySource
|
2014-08-29 15:35:53 +04:00
|
|
|
|
|
|
|
|
|
|
|
class NullableFKSourceSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = NullableForeignKeySource
|
|
|
|
|
|
|
|
|
|
|
|
class NullableFKSourceDetail(generics.RetrieveUpdateDestroyAPIView):
|
|
|
|
queryset = NullableForeignKeySource.objects.all()
|
|
|
|
serializer_class = NullableFKSourceSerializer
|
2014-02-26 21:08:26 +04:00
|
|
|
|
|
|
|
|
|
|
|
urlpatterns = patterns(
|
|
|
|
'',
|
|
|
|
url(r'^objects/(?P<pk>\d+)/$', NullableFKSourceDetail.as_view(), name='object-detail'),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class NullableForeignKeyTests(APITestCase):
|
|
|
|
"""
|
2014-02-27 21:17:32 +04:00
|
|
|
DRF should be able to handle nullable foreign keys when a test
|
|
|
|
Client POST/PUT request is made with its own serialized object.
|
2014-02-26 21:08:26 +04:00
|
|
|
"""
|
2014-03-02 15:40:30 +04:00
|
|
|
urls = 'tests.test_nullable_fields'
|
2014-02-26 21:08:26 +04:00
|
|
|
|
2014-02-27 21:17:32 +04:00
|
|
|
def test_updating_object_with_null_fk(self):
|
2014-02-26 21:08:26 +04:00
|
|
|
obj = NullableForeignKeySource(name='example', target=None)
|
|
|
|
obj.save()
|
|
|
|
serialized_data = NullableFKSourceSerializer(obj).data
|
|
|
|
|
|
|
|
response = self.client.put(reverse('object-detail', args=[obj.pk]), serialized_data)
|
|
|
|
|
|
|
|
self.assertEqual(response.data, serialized_data)
|