Problem with migration of the model with an embedded field
Migrating a model with EmbeddedModelField made AttributeError: module 'django.forms.widgets' has no attribute 'BlogForm' error.
Python script
I followed your tutorial https://nesdis.github.io/djongo/integrating-django-with-mongodb/
# models.py
from djongo import models
class Blog(models.Model):
name = models.CharField(max_length=100)
tagline = models.TextField()
class Meta:
abstract = True
class Entry(models.Model):
blog = models.EmbeddedModelField(
model_container=Blog,
)
headline = models.CharField(max_length=255)
# admin.py
from django.contrib import admin
from .models import Entry
admin.site.register(Entry)
and when I ran
python manage.py makemigrations
python manage.py runserver
I came across an error:
Traceback
Traceback (most recent call last):
File "manage.py", line 15, in
I came across the same problem. You mentioned in docs (https://nesdis.github.io/djongo/using-django-with-mongodb-data-fields/)
To display the embedded models in Django Admin, a Form for the embedded fields is required. Since the embedded field is an abstract model, the form for it can be easily created by using a ModelForm. The BlogForm defines Blog as the model with name and tagline as the form fields.
If you do not specify a ModelForm for your embedded models, and pass it using the model_form_class argument, Djongo will automatically generate a ModelForm for you.
But if we don't specify (like the above one of @Sashkow ) it is rasing AttributeError: module 'django.forms.widgets' has no attribute 'XXXForm'
Also there is a line in the sample code:
from djongo.models import forms
There is no package like this @nesdis
Is this happening only with Django Rest Framework? Also which version of Djongo do u use? Can you reproduce once with bare minimum code?
Also there is a line in the sample code:
from djongo.models import forms
Thanks for pointing that out!!
I solved the problem specifying a Form and pass it using the model_form_class argument. Here is the simple one for Blog class @Sashkow
class BlogForm(forms.ModelForm):
class Meta:
model = Blog
fields = [
'name',
'tagline',
]
Ad of course in Entry model, use it like this:
blog = models.EmbeddedModelField(
model_container=Blog,
model_form_class=BlogForm,
)
@nesdis I am not using Django Rest Framework. Problem is occurring with your sample codes that explain and use the embedded models. Here is my package versions from Requirement.txt:
dataclasses==0.5
Django==2.0.4
django-crispy-forms==1.7.2
djongo==1.2.23
Pillow==5.1.0
pymongo==3.6.1
pytz==2018.3
sqlparse==0.2.4
Thanks for your commitments.
This has been fixed now. There is no need to specify class argument anymore