CeraMix
CeraMix

Reputation: 121

Django Attribute Error - Form object has no attribute 'is_valid'

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..

Upvotes: 1

Views: 2501

Answers (2)

Zaeem Akhtar
Zaeem Akhtar

Reputation: 11

In your views.py file line 2

form = Car(request.POST):

change it to

form = CarForm(request.POST):

Hope it works!

Upvotes: 0

Alasdair
Alasdair

Reputation: 308789

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})

Upvotes: 4

Related Questions