1

I'm trying to save a object 'Animal' with the data from my form but im getting a error.

This is my view:

def addAnimal(request):
    if request.user.id == None:
        return render(request, "deny.html")
    else:
        if request.method == 'POST':
            animal_name = request.POST['animal_name']
            animal_type = request.POST['animal_type']
            animal_age = request.POST['animal_age']
            animal_port = request.POST['animal_port']
            animal = Animal(animal_name=animal_name, animal_age=animal_type, animal_type=animal_age, animal_port=animal_port)
            animal.save()
            return render(request, "home.html", args)

This is the model im trying to save:

class Animal(models.Model):
    animal_name = models.CharField(max_length=255)
    animal_age = models.ManyToManyField(Age)
    animal_type = models.ManyToManyField(Type)
    animal_port = models.ManyToManyField(Port)
    def __str__(self):
        return self.animal_name

And this is my form:

<form method="post" class="form-signin" action="{% url 'addAnimal' %}">
        {% csrf_token %}
        <img class="mb-4" src="{% static 'images/logo_transparent.png' %}" alt="" width="300" height="300">
        <h1 class="h3 mb-3 font-weight-normal text-white">Adicione o seu Animal de Estimação</h1>

        <input type="text" id="animal_name" name="animal_name" class="form-control"
               placeholder="Nome do Seu Animal de Estimação">

        <select class="form-control" id="animal_type" name="ration_type">
            <option value="Cão">Cão</option>
            <option value="Gato">Gato</option>
        </select>
        <select class="form-control" id="animal_age" name="animal_age">
            <option value="Junior">Junior</option>
            <option value="Adulto">Adulto</option>
            <option value="Senior">Senior</option>
        </select>
        <select class="form-control" id="animal_port" name="animal_port">
            <option value="Pequeno">Pequeno</option>
            <option value="Médio">Médio</option>
            <option value="Grande">Grande</option>
        </select>
        <div> &nbsp</div>
        <button class="btn btn-lg btn-primary btn-block" type="submit">Adicionar</button>
    </form>

And a print if you nedd so: Print of the Form

Update Im getting a new error:

TypeError at /addAnimal/
Direct assignment to the forward side of a many-to-many set is prohibited. Use animal_age.set() instead.

How can I solve it?

CalMac
  • 449
  • 1
  • 4
  • 30
Miguel Rodrigues
  • 311
  • 1
  • 3
  • 11

1 Answers1

1

This is because of your POST request.

Try request.POST.get() instead. And/or when you're taking in attributes from POST, try typing for example request.POST['animal name', None] or request.POST.get('animal name', None) Where None can be replaced with any default value. It's a null checking issue I think.

CalMac
  • 449
  • 1
  • 4
  • 30
  • In the future you should ask a new question if you're having a different error, but [here is a link to something that may solve your new problem](https://stackoverflow.com/questions/50015204/direct-assignment-to-the-forward-side-of-a-many-to-many-set-is-prohibited-use-e) – CalMac Jan 23 '20 at 08:08