jeudi 4 février 2021

Django RF. how to get a Django Model with nested relationship

While testing my features in a Django Rest framework App, I need to get an object, let's call it Foo and this object has some nested relationships. I can get it by my making a request with the APIClient as such :

class FooTest(TestCase):

  def setUp(self):
    self.client = APIClient()

  def test_foo_feature(self):
    foo_id = generator.generateFoo().id
    foo = self.client.get(reverse('foo-detail', args=[foo_id])).data

I was wondering if I could call directly my FooSerializer in a certain way to get my Foo object with the nested relationships, instead of passing by the view with the help of the APIClient because simply calling Foo.objects.get(id=foo_id) doesn't return the nested relationships.

Aucun commentaire:

Enregistrer un commentaire