0

I'm new to Django in Python. I have a python Django project called "corr_end" with an app within it called "send_values"

I wrote a serializer, trying to make get/put/post/delete methods available and working through Postman testing.

When I try the get method, it doesn't work in Postman and gives me an error.

I appreciate any help on this. Thank you.

URL: http://127.0.0.1:8000/values/dinos

Error from terminal:

Error from terminal:

Internal Server Error: /values/dinos TypeError: __init__() takes 1 positional argument but 2 were given [19/Mar/2020 07:02:00] "GET /values/dinos HTTP/1.1" 500 63860

Traceback (most recent call last): File "[redacted]/anaconda3/envs/chipseq/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner response = get_response(request) File "[redacted]/anaconda3/envs/chipseq/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response response = self.process_exception_by_middleware(e, request) File "[redacted]/anaconda3/envs/chipseq/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs)

I created a Python package 'api' under send_values directory with send_values_api.py containing the serializer.

from rest_framework.exceptions import ValidationError
from rest_framework.serializers import ModelSerializer
from rest_framework.viewsets import ModelViewSet
from ..models import Dinosaur

class DinoSerializer(ModelSerializer):
    class Meta:
        model = Dinosaur
        fields = ['name', 'age', 'species']

    def validate(self, userData):
        if not userData['name']:
            print('name is required')
            return ValidationError
        return userData

    def create(self, userData):
        newDinosaur = Dinosaur.objects.create(**userData)
        newDinosaur.save()
        return newDinosaur

    def update(self, existingDinosaur, userData):
        fields = ['name', 'age', 'species']
        for i in fields:
            fieldValue = userData.get(i, getattr(existingDinosaur, i))
            setattr(existingDinosaur, i, fieldValue)
        existingDinosaur.save()
        return existingDinosaur

class DinoViewSet(ModelViewSet):
    serializer_class = DinoSerializer
    http_method_names = ['get', 'post', 'put', 'delete', 'options']
    queryset = Dinosaur.objects.all()

models.py file:

from django.db import models
from django.utils import timezone

# Create your models here.
class Correlations(models.Model):
    message = models.CharField(max_length=100)
    score = models.TextField()
    executed = models.CharField(max_length=100)
    created_at = models.DateTimeField(default=timezone.now)

class Dinosaur(models.Model):
    age = models.PositiveIntegerField()
    species = models.TextField()
    name = models.CharField(max_length=50)

    def __str__(self):
        return self.name

urls.py in send_values:

from .api.send_values_api import DinoViewSet
from django.conf.urls import url

urlpatterns = [
    url('dinos', DinoViewSet),
]

Note: There's no code in the views.py file.

1 Answer 1

2

ViewSets are not same as Views. You can't add them directly to the urls like Views. Easiest solution here is to use routers like this:

from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register(r'dinos', DinoViewSet)

urlpatterns = [
    path('', include(router.urls)),
]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.