[Django]-Object has no attribute 'get'

101👍

Your problem is here:

intention = Intention.objects.get(pk=id)
form = IntentionForm(intention) # An unbound form

The first argument to a form is the data but you are passing the instance. To properly pass the instance you should use:

intention = Intention.objects.get(pk=id)
form = IntentionForm(instance=intention) # An unbound form

32👍

The above answer is correct, however, this error can also be generated by incorrectly passing arguments in the init of a form, which is used for an admin model.

Example:

class MyForm(forms.ModelForm):
  def __init__(self, *args, **kwargs):
    super(MyForm, self).__init__(self, *args, **kwargs)

Notice the double passing of self? It should be:

class MyForm(forms.ModelForm):
  def __init__(self, *args, **kwargs):
    super(MyForm, self).__init__(*args, **kwargs)

8👍

In Django, be careful how you name your views and models.

In my case, I had this in models.py:

class Contact(models.Model):
    ...

In views.py, I had:

def contact(request):
    ...

Then, in urls.py, I had:

from .views import Contact

So, I was actually importing the model class, and not the contact function, so my error was:

'Contact' object has no attribute 'get'

That object has no attribute get. That’s suppose to come from views.py, not a model.

Leave a comment