How to Solve Error Message TypeError: ‘ModelBase’ object is not subscriptable when Running Django Application

Posted on

Introduction

This is an article where the focus is to solve a problem. It is by solving an error when the Django-based application execution suddenly appear. Before going on further, the following is the output of the Django-based application execution which ends with the appearance of an error message as follows :

class OrganizationForm(forms.ModelForm):
File "C:\production\server\program\project\apps\forms.py", line 15, in OrganizationForm
program = forms.CharField(label = 'Program', widget = forms.Select(choices=Organization['program']))
TypeError: 'ModelBase' object is not subscriptable

Actually, the above script code is a script to define a form with a dropdown list. But apparently, feeding the dropdownlist with an object with a specific attribute to the Select widget only causing an error message.

So, what is the cause of the error message ?. Basically, the following are several important scripts which is triggering it. The first one is the file with the name ‘forms.py’. Part of the content exist as follows :

class OrganizationForm(forms.ModelForm):
    program = forms.CharField(label = 'Program', widget = forms.Select(choices=Organization['program']))
    activity = forms.CharField(label = 'Activity', widget = forms.Select(choices=Activity['name']))
    class Meta:
        model = Organization
        fields = ('unit',)

The other one is another file with the name of ‘models.py’. It is the class which is a model and the definition of those classes exist in ‘models.py’ file. The following are the contents of that file :

class Activity(models.Model):
    id = models.AutoField(primary_key=True)
    code = models.CharField(max_length=10, blank=True, null=True)
    name = models.TextField()
    class Meta:
        verbose_name_plural = "Activities"
    def __str__(self):
    #    return self.name
        return '%s %s' %(self.code, self.name)

class Organization(models.Model):
    unit = models.CharField(db_column='unit', max_length=100)
    program = models.TextField(blank=True, null=True,)
    activity = models.ForeignKey('Activity', blank=True, null=True, on_delete=models.CASCADE)
    is_aktif = models.BooleanField()
    def __str__(self):
        return self.unit
    def save(self, *args, **kwargs):
        return super().save(*args, **kwargs)
    class Meta:
        verbose_name_plural = 'Organizations'
        db_table = 'apps_organization'
    def __unicode__(self):
        return self.unit

Solution

Just focus to the error message. It is the line with the following error message :

TypeError: 'ModelBase' object is not subscriptable

The above line describing that the object cannot be the value for the Select widget. So, how can a form is using a class object to appear in a dropdown list ?. Actually, the following is the solution for printing or displaying the dropdown list by defining it in the form as follows :

activity = forms.ModelChoiceField(queryset = Activity.objects.all(), initial = 0)

The revision of the form class in the forms.py file in the complete form will be as follows :

class OrganizationForm(forms.ModelForm):
    program = forms.ModelChoiceField(label = 'Program', widget = forms.Select(choices=Organization['program']))
    activity = forms.ModelChoiceField(label = 'Activity', widget = forms.Select(choices=Activity['name']))
    class Meta:
        model = Organization
        fields = ('unit',)

The Django template file for rendering the dropdown list will be in the following :

   <form method="post"id="activityForm" action="{% url 'org_activity_save' %}">
        {% csrf_token %}
        {{ form.as_table }}               
        <button type="submit">Submit</button>
   </form>

One thought on “How to Solve Error Message TypeError: ‘ModelBase’ object is not subscriptable when Running Django Application

Leave a Reply