• Wir präsentieren Dir heute ein Stellenangebot für einen Frontend-Entwickler Angular / Java in Braunschweig. Hier geht es zur Jobanzeige

python – getting bad request on tests using django rest framework

G

gorgenasty

Neues Mitglied
I'm learning django rest framework and I'm trying and a test get me a bit nervous, so if someone can help me I would appreciate
my tests.py file:
Python:
    def test_post_method (self):
url = '/ arvores /'
self.especie = Especies.objects.create (descricao = 'Citrus aurantium')
data = {'descricao': 'Laranjeira', 'especie': self.especie.id , 'idade': 12}
response = self.client.post (url, data, format = 'json')
self.assertEqual (response.status_code, status.HTTP_201_CREATED)

my views.py file:
Python:
class EspeciesViewSet (viewsets.ModelViewSet):

queryset = Especies.objects.all ()
serializer_class = EspeciesSerializer

class ArvoresViewSet (viewsets.ModelViewSet):

queryset = Arvores.objects.all ()
serializer_class = ArvoresSerializer

serializer.py file:
Python:
class EspeciesSerializer (serializers.HyperlinkedModelSerializer):
class Meta:
model = Especies
fields = ['id', 'descricao']


class ArvoresSerializer (serializers.HyperlinkedModelSerializer):

class Meta:
model = Arvores
fields = ['id', 'especies', 'descricao', 'idade']

and models.py:
Python:
class Especies (models.Model):
descricao = models.CharField (verbose_name = 'Descrição', max_length = 255)

class Arvores (models.Model):
especies = models.ForeignKey (Especies, on_delete = models.CASCADE)
descricao = models. CharField (verbose_name = 'Descrição', max_length = 255)
idade = models.PositiveSmallIntegerField ()
 
Zuletzt bearbeitet von einem Moderator:
krgewb

krgewb

Top Contributor
my tests.py file:
Python:
def test_post_method (self):
  url = '/ arvores /'
  self.especie = Especies.objects.create (descricao = 'Citrus aurantium')
  data = {'descricao': 'Laranjeira', 'especie': self.especie.id , 'idade': 12}
  response = self.client.post (url, data, format = 'json')
  self.assertEqual (response.status_code, status.HTTP_201_CREATED)

views.py file:
Python:
class EspeciesViewSet (viewsets.ModelViewSet):

  queryset = Especies.objects.all ()
  serializer_class = EspeciesSerializer

class ArvoresViewSet (viewsets.ModelViewSet):

  queryset = Arvores.objects.all ()
  serializer_class = ArvoresSerializer

serializer.py file:
Python:
class EspeciesSerializer (serializers.HyperlinkedModelSerializer):
  class Meta:
    model = Especies
    fields = ['id', 'descricao']

class ArvoresSerializer (serializers.HyperlinkedModelSerializer):
  class Meta:
    model = Arvores
    fields = ['id', 'especies', 'descricao', 'idade']

models.py:
Python:
class Especies (models.Model):
  descricao = models.CharField (verbose_name = 'Descrição', max_length = 255)

class Arvores (models.Model):
  especies = models.ForeignKey (Especies, on_delete = models.CASCADE)
  descricao = models. CharField (verbose_name = 'Descrição', max_length = 255)
  idade = models.PositiveSmallIntegerField ()
 

Ähnliche Java Themen

Anzeige

Neue Themen


Oben