how to solve this simple Django CRUD programming problem?

  django, python

Hi, i am tryting to solve this problem but i don’t seem to get anywhere, when i create objects to the database i’m used to have a form, also i am not sure how to return a jsonreponse in django, can you help me?

Here is the problem:

You are writing a simple CRUD application in Django (2.0.5) for managing a zoo. It has to allow particular animals to be added and removed, and to track the time when they were last fed. Animals must have names that are unique across all species. Unfortunately, the creator of this model didn’t provide unique constraints regarding the name at the database level, so you need to take this into account in the views.

Models are implemented in the following way:

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

class Species(models.Model):
    name = models.CharField(max_length=30, primary_key=True)

class Animal(models.Model):
    name = models.CharField(max_length=30)
    species = models.ForeignKey(Species, on_delete=models.CASCADE)
    last_feed_time = models.DateTimeField(


  1. Implement a GET request handler in the class-based view AnimalPopulationView that returns the total number of animals in the zoo.

    1. It should return an HTTPResponse with a number.
  2. Implement a GET request handler in the class-based view AnimalView that returns data about an animal in JSON format.

    1. it should return data in the following format: {"name" : "name", "species" : "species", "last_feed_time" : "last_feed_time"}
    2. You should query animals using their name, which is passed as GET parameter name.
    3. If there is no animal of the specified name, the view should return a response with a 404 status.
  3. implement a POST request handler in the class-based view AnimalView that creates an animal

    1. Perform the unique name validation there. Return the data relating to the created animal in the same format as in requirement 2.
    2. perform the name and species length validation there (for example, the model’s maximum name length should be 30 characters).
    3. if the animal was added successfully, return a status code of 201 or 422 otherwise.
    4. if the animal "species" you are creating doesn’t exist, it should be created.
    5. The request will include the parameters ["name", "species"] passed as POST parameters.
  4. Implement a GET request handler in the class-based view HungryAnimalsView that returns animals that have not beed fed for at least two days.

    1. It should return an HttpResponse with a number.
  5. implement a POST request handler in the class-based view FeedAnimalView that updates an animal’s last_feed_time to the current time.

    1. To set the current time, use timezone from django.utils.
    2. The request will include the parameter ["name"] passed as POST parameters.

Here is the starter code:

from django.http import HttpResponse, JsonResponse
from django.views.generic import View
from django.utils import timezone

from .models import Animal, Species

class AnimalPopulationView(View):

    def get(self, request):
        animals = Animal.objects.count()
        return HttpResponse(animals)

class AnimalView(View):

    def get(self, request):
        return JsonResponse({})

    def post(self, request):
        return JsonResponse({})

class HungryAnimalsView(View):

    def get(self, request):
        return HttpResponse()

class FeedAnimalView(View):

    def post(self, request):
        return HttpResponse()

i’ll really appreciate the help.

Source: Python Questions