1

I am receiving an error, object has no attribute 'is_valid', when trying to insert form data into a form. Below is the structure of my code:

Views.py:

def add_user(request):
    form = Car(request.POST)
    if request.method == 'POST' and form.is_valid():
        last_store = form.cleaned_data.get['value']
        make = request.POST.get('make', '')
        model = request.POST.get('model', '')
        ..

        car_obj = Car(last_store = last_store, make = make, model = model, series = series, series_year = series_year, price_new = price_new, engine_size = engine_size, fuel_system = fuel_system, tank_capacity = tank_capacity, power = power, seating_capacity = seating_capacity, standard_transmission = standard_transmission,  body_type = body_type, drive = drive, wheelbase = wheelbase, available = available)
        car_obj.save()
        return HttpResponseRedirect('/inventory/add/')
    else:
        form = Car()

    return render(request, 'cars/inventory-add.html', {})

Class:

class Car(models.Model):
    make = models.CharField(max_length=200, null=False)
    model = models.CharField(max_length=200, null=False)
    series = models.CharField(max_length=200, null=False)
    series_year = models.IntegerField(null=False)
    price_new = models.IntegerField(null=False)
    ..
    last_store = models.ForeignKey(Store, on_delete=models.DO_NOTHING, related_name="last_store")
    available = models.BooleanField(null=False, default=True)

    def __str__(self):
        return(self.make + " " + self.model)

CarForm (forms.py)

class CarForm(forms.ModelForm):
    class Meta:
        model = Car
        fields = ['last_store', 'make', 'model', 'series', 'series_year', 'price_new', 'engine_size', 'fuel_system', 'tank_capacity', 'power', 'seating_capacity', 'standard_transmission', 'body_type', 'drive', 'wheelbase', 'available']

The error lies within the last_store as without form.is_valid(), apparently I cannot use form.cleaned_data, but form.is_valid() seems to not even exist? As seen in the class, last_store is of type a foreign key, therefore I am struggling to set the default value for it when the user enters input into the form. The last_store variable is attempting to fetch the value from the inputted form data which consists of a SELECT option..

2 Answers 2

4

You have a clash between your model and form, which are both named Car. Typically you would fix this by renaming the form CarForm.

Note that you shouldn't normally need to create the object with car_obj = Car(...). If you use a model form, you can simplify your code to car_obj = form.save(). You should also move CarForm(request.POST) inside the if request.method == 'POST' check, and include form in the template context. Putting that together, you get something like:

def add_user(request):
    if request.method == 'POST':
        form = CarForm(request.POST)
        if form.is_valid():
            car_obj = form.save()
            return HttpResponseRedirect('/inventory/add/')
    else:
        form = CarForm()
    return render(request, 'cars/inventory-add.html', {'form': form})
Sign up to request clarification or add additional context in comments.

5 Comments

How could I create a model form? CarForm is currently undefined.
Please check updated code. It doesn't crash anymore which is sweet, but still not inserting into database. Could you see why this is? I'm not sure if my CarForm is correct or missing anything.
Try debugging by checking the values of form.is_valid() and form.errors in the view.
yes it is printing that the form is not valid. The Car could not be created because the data didn't validate.
Then check form.errors as I suggested, which will tell you why the data did not validate.
0

In your views.py file line 2

form = Car(request.POST):

change it to

form = CarForm(request.POST):

Hope it works!

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.