mardi 11 juin 2019

Django test: DRF api cannot access the object created by the Model.objects.create method

I'm writing unittest for my project. I use DjangoRestFramework.

And in one of my test case method, I created an object using the Model.objects.create() method, but when I use self.client.get('/api/model/{id}/') after that, I got a 404 Not Found response.

def test_0002_company_update_username(self):
    company = m.Company.objects.create(name='CompanyName')
    # the following line prints out the object correctly
    print(m.Company.objects.all())
    resp = self.client.get('/api/company/{}/'.format(company.id))
    # whlile the below line yields a `404` response, but we expected to get the object we just created, why?
    print(resp, resp.status_code)

Aucun commentaire:

Enregistrer un commentaire