DigFor
DigFor

Reputation: 13

Django forms. TypeError: __init__() got an unexpected keyword argument 'instance'

I keep getting error messages and have no idea why. I think it has to do with the variable for instance, but i see a lot of examples all over the internet that work the same way.

models.py

class Establishments(models.Model):
    title = models.CharField(max_length=255)
    town = models.ForeignKey(Town, on_delete=SET_NULL, null=True)
    addrstreet = models.CharField(max_length=255)
    addrzip = models.CharField(max_length=12)
    telephone = models.CharField(max_length=15)
    email = models.CharField(max_length=255)
    chamberofcomnr = models.CharField(max_length=25)
    description = models.TextField(max_length=255)
    website =  models.CharField(max_length=255)
    categorie = models.ForeignKey(Establishmentcategory, on_delete=SET_NULL, null=True)
    pub_date = models.DateTimeField('date published')
    drupuser = models.ForeignKey(Drupalusers, on_delete=SET_NULL, null=True)
    druppublished = models.BooleanField()
    drupurl = models.CharField(max_length=255)
    drupnodeid = models.IntegerField()
    def __str__(self):
        return self.title
class Impcalendar(models.Model):
    establishment = models.ForeignKey(Establishments, on_delete=SET_NULL, null=True)
    active = models.BooleanField()
    prio = models.IntegerField()
    url = models.CharField(max_length=255)
    check_intervalh = models.IntegerField()
    check_fixedh = models.IntegerField()
    log = models.BooleanField()
    cuttag = models.CharField(max_length=255)
    cuttxt =  models.CharField(max_length=255)
    cuttxtend = models.CharField(max_length=255)
    comment = models.CharField(max_length=255)
    page = models.TextField()
    pageold = models.TextField()    
    change = models.TextField()
    pagedate = models.DateTimeField()
    pagedatenext = models.DateTimeField()
    status = models.IntegerField()
    errors = models.IntegerField()
    def __str__(self):
        return str(self.id)

urls.py

path('calendar/<int:calendar_id>/', views.calendaredit, name='calendaredit')

views.py

def calendaredit(request, calendar_id):
    calendar = get_object_or_404(Impcalendar, pk=calendar_id)
    print (calendar.url)
    form = ImpcalendarForm(request.POST or None, instance=calendar)
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        print (form.url)
        # check whether it's valid:
        if form.is_valid():
            #calendar.establishment = form.cleaned_data['
            calendar = form.save(commit=false)
            calendar.active = form.cleaned_data['active']
            calendar.save()
            return redirect('handmatig')

return render(request, 'import_calendar/handmatig_edit.html', {'form': form})

forms.py

class ImpcalendarForm(forms.Form):
    establishment = forms.ModelChoiceField(queryset = Establishments.objects.all())
    page = forms.CharField(widget=forms.Textarea)
    pageold = forms.CharField(widget=forms.Textarea)
    change = forms.CharField(widget=forms.Textarea)
    class Meta:
        model = Impcalendar
        fields = '__all__'

So i want to have a record page, listing all the records already works, where i can edit the form. It needs to show the record as a Django form. It crashes on the line; form = ImpcalendarForm(request.POST or None, instance=calendar)

If i print the variable calendar or calendar.url i get the correct data. The error message is;

TypeError: __init__() got an unexpected keyword argument 'instance'

Spend a week debugging. Now escalading. ;-)

Upvotes: 1

Views: 5294

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 600059

Your form subclasses forms.Form instead of forms.ModelForm.

Normal forms don't take model instances, and neither do they have an inner Meta class.

Upvotes: 11

Related Questions