djongo icon indicating copy to clipboard operation
djongo copied to clipboard

Problem with migration of the model with an embedded field

Open Sashkow opened this issue 8 years ago • 5 comments

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 execute_from_command_line(sys.argv) File "/home/sashkoah/Python/whizzy-restful-server/env/lib/python3.6/site-packages/django/core/management/init.py", line 371, in execute_from_command_line utility.execute() File "/home/sashkoah/Python/whizzy-restful-server/env/lib/python3.6/site-packages/django/core/management/init.py", line 365, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/home/sashkoah/Python/whizzy-restful-server/env/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv self.execute(*args, **cmd_options) File "/home/sashkoah/Python/whizzy-restful-server/env/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute output = self.handle(*args, **options) File "/home/sashkoah/Python/whizzy-restful-server/env/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 79, in handle executor = MigrationExecutor(connection, self.migration_progress_callback) File "/home/sashkoah/Python/whizzy-restful-server/env/lib/python3.6/site-packages/django/db/migrations/executor.py", line 18, in init self.loader = MigrationLoader(self.connection) File "/home/sashkoah/Python/whizzy-restful-server/env/lib/python3.6/site-packages/django/db/migrations/loader.py", line 49, in init self.build_graph() File "/home/sashkoah/Python/whizzy-restful-server/env/lib/python3.6/site-packages/django/db/migrations/loader.py", line 200, in build_graph self.load_disk() File "/home/sashkoah/Python/whizzy-restful-server/env/lib/python3.6/site-packages/django/db/migrations/loader.py", line 109, in load_disk migration_module = import_module("%s.%s" % (module_name, migration_name)) File "/home/sashkoah/Python/whizzy-restful-server/env/lib/python3.6/importlib/init.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 994, in _gcd_import File "", line 971, in _find_and_load File "", line 955, in _find_and_load_unlocked File "", line 665, in _load_unlocked File "", line 678, in exec_module File "", line 219, in _call_with_frames_removed File "/home/sashkoah/Python/whizzy-restful-server/whizzy/restapi/migrations/0001_initial.py", line 9, in class Migration(migrations.Migration): File "/home/sashkoah/Python/whizzy-restful-server/whizzy/restapi/migrations/0001_initial.py", line 21, in Migration ('blog', djongo.models.fields.EmbeddedModelField(model_container=restapi.models.Blog, model_form_class=django.forms.widgets.BlogForm, null=True)), AttributeError: module 'django.forms.widgets' has no attribute 'BlogForm'

Sashkow avatar Apr 01 '18 17:04 Sashkow

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

deeptechs avatar Apr 05 '18 10:04 deeptechs

Is this happening only with Django Rest Framework? Also which version of Djongo do u use? Can you reproduce once with bare minimum code?

nesdis avatar Apr 05 '18 10:04 nesdis

Also there is a line in the sample code:

from djongo.models import forms

Thanks for pointing that out!!

nesdis avatar Apr 05 '18 10:04 nesdis

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.

deeptechs avatar Apr 05 '18 11:04 deeptechs

This has been fixed now. There is no need to specify class argument anymore

nesdis avatar Apr 08 '18 06:04 nesdis