djongo icon indicating copy to clipboard operation
djongo copied to clipboard

Getting error while saving model from admin having nested embedded documents

Open setushwetank opened this issue 6 years ago • 13 comments

I have a model which has nested embedded documents. While saving from admin UI following error is raised - 'list' object has no attribute '_meta' Model looks like below , while debugging , it was found that 2nd level of nested document is raising exception. If contents of dimensions is moved under shipping , this works fine { "productId": "P1020", "productName": "Product_52956", "shipping": { "dimensions": { "height": 8.2787, "length": 7.3324, "width": 4.6937 }, "weight": 2.2756 } }

Python script

from djongo import models

class Dimensions(models.Model):
    height = models.FloatField()
    length = models.FloatField()
    width = models.FloatField()

    class Meta:
        abstract = True


class Shipping(models.Model):
    weight = models.FloatField()

    dimensions = models.EmbeddedModelField(
        model_container=Dimensions,
    )

    class Meta:
        abstract = True


class Supplier(models.Model):
    name = models.CharField(max_length=20)
    inventory = models.IntegerField()

    class Meta:
        abstract = True

    def __str__(self):
        return self.name


class Product(models.Model):
    productId = models.CharField(max_length=10)
    productName = models.CharField(max_length=20)
    price = models.FloatField()
    rating = models.CharField(max_length=20)

    shipping = models.EmbeddedModelField(
        model_container=Shipping,
    )

    category = models.CharField(max_length=20)
    brand = models.CharField(max_length=20)

    supplier = models.ArrayModelField(
        model_container=Supplier,
    )

    def __str__(self):
        return "Product Id: "+self.productId + " Product Name: " + self.productName

Traceback

Internal Server Error: /admin/CatalogApp/product/add/ Traceback (most recent call last): File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\core\handlers\exception.py", line 35, in inner response = get_response(request) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\core\handlers\base.py", line 158, in _get_response response = self.process_exception_by_middleware(e, request) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\core\handlers\base.py", line 156, in _get_response response = response.render() File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\response.py", line 106, in render self.content = self.rendered_content File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\response.py", line 83, in rendered_content content = template.render(context, self._request) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\backends\django.py", line 61, in render return self.template.render(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\base.py", line 175, in render return self._render(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\base.py", line 167, in _render return self.nodelist.render(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\base.py", line 943, in render bit = node.render_annotated(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\base.py", line 910, in render_annotated return self.render(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\loader_tags.py", line 155, in render return compiled_parent._render(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\base.py", line 167, in _render return self.nodelist.render(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\base.py", line 943, in render bit = node.render_annotated(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\base.py", line 910, in render_annotated return self.render(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\loader_tags.py", line 155, in render return compiled_parent._render(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\base.py", line 167, in _render return self.nodelist.render(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\base.py", line 943, in render bit = node.render_annotated(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\base.py", line 910, in render_annotated return self.render(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\loader_tags.py", line 67, in render result = block.nodelist.render(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\base.py", line 943, in render bit = node.render_annotated(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\base.py", line 910, in render_annotated return self.render(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\loader_tags.py", line 67, in render result = block.nodelist.render(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\base.py", line 943, in render bit = node.render_annotated(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\base.py", line 910, in render_annotated return self.render(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\defaulttags.py", line 211, in render nodelist.append(node.render_annotated(context)) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\base.py", line 910, in render_annotated return self.render(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\loader_tags.py", line 194, in render return template.render(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\base.py", line 177, in render return self._render(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\base.py", line 167, in _render return self.nodelist.render(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\base.py", line 943, in render bit = node.render_annotated(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\base.py", line 910, in render_annotated return self.render(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\defaulttags.py", line 211, in render nodelist.append(node.render_annotated(context)) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\base.py", line 910, in render_annotated return self.render(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\defaulttags.py", line 211, in render nodelist.append(node.render_annotated(context)) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\base.py", line 910, in render_annotated return self.render(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\defaulttags.py", line 314, in render return nodelist.render(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\base.py", line 943, in render bit = node.render_annotated(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\base.py", line 910, in render_annotated return self.render(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\defaulttags.py", line 314, in render return nodelist.render(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\base.py", line 943, in render bit = node.render_annotated(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\base.py", line 910, in render_annotated return self.render(context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\base.py", line 999, in render return render_value_in_context(output, context) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\template\base.py", line 978, in render_value_in_context value = str(value) File "C:\Users\611834094\AppData\Roaming\Python\Python36\site-packages\djongo\models\fields.py", line 568, in str return mark_safe(f'

\n{ model_form.as_table() }\n
') File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\forms\forms.py", line 279, in as_table errors_on_separate_row=False) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\forms\forms.py", line 238, in _html_output 'field_name': bf.html_name, File "C:\Users\611834094\AppData\Roaming\Python\Python36\site-packages\djongo\models\fields.py", line 566, in str model_form = self.field.model_form_class(instance=instance, **self.field.model_form_kwargs) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\forms\models.py", line 291, in init object_data = model_to_dict(instance, opts.fields, opts.exclude) File "C:\Users\611834094\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.2-py3.6.egg\django\forms\models.py", line 82, in model_to_dict opts = instance._meta AttributeError: 'list' object has no attribute '_meta' [22/May/2018 21:00:52] "POST /admin/CatalogApp/product/add/ HTTP/1.1" 500 402143

setushwetank avatar May 22 '18 16:05 setushwetank

It seems that djongo (or Django itself, which the abstract attribute is coming from) does not allow to embed an abstract model inside another abstract model (ie, max deep <= 2), although there are not such limitations in MongoDB and pymongo

leonardobareno avatar Nov 10 '18 01:11 leonardobareno

Any way to get over this limitation?

gprearo avatar Jul 16 '19 14:07 gprearo

I am facing the same problem, does anyone have a solution?

lesantana09 avatar Jul 25 '19 22:07 lesantana09

I am facing the same problem, does anyone have a solution?

anwar-moj avatar Aug 19 '19 23:08 anwar-moj

I am facing the same problem, does anyone have a solution?

tsotnesharvadze avatar Dec 30 '19 17:12 tsotnesharvadze

I am facing the same problem, does anyone have a solution?

namper avatar Dec 30 '19 17:12 namper

I am facing the same problem, does anyone have a solution?

Raynur2000 avatar Jul 02 '20 07:07 Raynur2000

still didnt solve it? :(

hypy13 avatar Oct 20 '20 17:10 hypy13

@nesdis can you help us? Is there a way around this? Thanks.

brunowego avatar Nov 02 '20 17:11 brunowego

@nesdis now the django admin is messed up. Is therere any way to fix this? Thanks!

aryaniyaps avatar Jun 03 '21 07:06 aryaniyaps

@aryan340 This package is mostly unusable, just use Pymongo. however check your Django version. If it's Django 3+ this might have broke djongo integration.

namper avatar Jun 03 '21 08:06 namper

But then I would lose the django orm support - which is the main reason anyone would want to use django in the first place..

aryaniyaps avatar Jun 03 '21 11:06 aryaniyaps

But then I would lose the django orm support - which is the main reason anyone would want to use django in the first place..

Then why are you using mongoDB? if you must, then why not use database routing, and use mongo for certain parts of ur application.

namper avatar Jun 03 '21 17:06 namper

guys, I really don't understand this but when I change the name of EmbededField in my model (without migrations) I have access to the rest of the instances of that model in admin UI! But when I create an instance with this new name I face this error again

MrAyubi avatar Feb 01 '22 11:02 MrAyubi