When writing tests for Django Rest Framework, how are hyperlinked relations supposed to be created?
I'm trying to create a Foo object with a related Bar object, but I'm not sure how I can most efficiently create that relation.
# models.py
import uuid
from django.db import models
class Foo(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4)
bar = models.ForeignKey('Bar')
class Bar(models.Model):
BarUUID = models.UUIDField(primary_key=True, default=uuid.uuid4
# serializers.py
from rest_framework import serializers
from myapp.models import Bar, Foo
class FooSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Foo
fields = ('url', 'id', 'bar')
class BarSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Bar
fields = ('url', 'BarUUID')
# tests.py
from rest_framework.reverse import reverse
from rest_framework.test import APITestCase
class FooTests(APITestCase):
def test_create_foo(self):
bar = Bar.objects.create()
url = reverse('foo-list')
data = {
'bar': # how do I get the URL?
}
response = self.client.post(url, data, format='json')
Aucun commentaire:
Enregistrer un commentaire