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)
- [Django]-Django: Make certain fields in a ModelForm required=False
- [Django]-Django : Can't import 'module'. Check that module AppConfig.name is correct
- [Django]-How to submit form without refreshing page using Django, Ajax, jQuery?
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
.
- [Django]-Django, ImportError: cannot import name Celery, possible circular import?
- [Django]-Generate unique id in django from a model field
- [Django]-Can one use the Django database layer outside of Django?
Source:stackexchange.com