Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am getting __init__() got an unexpected keyword argument 'instance' with CreateView

I am using Django 2.1 version. I am trying to make CreateView. Here is my views.py;

# views.py
class CreateJob(CreateView):
    form_class = CreateJob
    template_name = 'category_list.html'

and here is my forms.py;

# forms.py
from django import forms
from myapp.models import Category

class CreateJob(forms.Form):
    CATEGORY_CHOICES = (
    )
    category_list = forms.ChoiceField(
        widget=forms.Select(attrs={
            "id": "cate"
        }),
        choices=CATEGORY_CHOICES,
        required=True,
    )

    def __init__(self, *args, **kwargs):
        super(CreateJob, self).__init__(*args, **kwargs)
        category_choices = [(cat.id, cat.name) for cat in Category.objects.all()]
        self.fields['category_list'].choices = category_choices

in this forms.py I am trying to make choicefield and list category objects. and in my template, when i select category, it will make ajax request and will list description list which is related to Category model. you can look my template below. once I select one of categories which is created via choicefield in forms.py, it will list description list in option which has "desc" id.

{% extends '../base.html' %}

{% block body %}
   <form action="." method="post">
   {% csrf_token %}
   {{ form }}
   </form>

  <select id="desc">
    {% for description in description_list %}
      <option value="{{ description.pk }}">{{ description.name }}></option>
    {% endfor %}
  </select>
{% endblock %}

{% block script %}
  <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
  <script>
    $(document).ready(function (e) {
      $("#cate").change(function(e) {
        cat = $(this).val();
        var job = $("#desc");
        $.ajax({
          url: 'item/' + cat + '/',
          type: "GET",
          processData: false,
                contentType: false,
                success: function (data) {
                  console.log(data.description_list),
                  job.html(data)
                },
                error: function (xhr, errmsg, err) {
                    console.log(xhr, errmsg, err);
                } // end error: function
            });
        });
    });
  </script>
{% endblock %}

and my urls.py is looks like this;

path('test/', CreateJob.as_view()),

when I proceed to 127.0.0.1:8000/test, it gives me this error:

File "C:\Users\kenan\Desktop\Django\jobs\myapp\forms.py", line 16, in __init__
super(CreateJob, self).__init__(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'instance'
[23/Aug/2018 14:37:03] "GET /test/ HTTP/1.1" 500 89443

I want to note that, I searched for it and some people fix their problem by changing forms.Form to forms.ModelForm. but that's not what I want. Thanks in advance!

like image 969
Kenan Avatar asked Oct 25 '25 05:10

Kenan


1 Answers

You are using a CreateView but the form you associate with it is not a ModelForm. ModelForm takes an optional instance arg (used for editing an existing model instance) - which is not a valid argument for non-model forms -, and CreateView passes this argument to your form class when instanciating it. Hence the error.

TL;DR : if you want to use a CreateView, you must use a ModelForm (or something that exposes the exact same API as a ModelForm).

like image 135
bruno desthuilliers Avatar answered Oct 26 '25 17:10

bruno desthuilliers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!