doableware / djongo

Django and MongoDB database connector
https://www.djongomapper.com
GNU Affero General Public License v3.0
1.87k stars 353 forks source link

Getting error while saving model from admin having nested embedded documents #136

Open setushwetank opened 6 years ago

setushwetank commented 6 years ago

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

leonardobareno commented 5 years ago

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

gprearo commented 5 years ago

Any way to get over this limitation?

lesantana09 commented 5 years ago

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

anwar-moj commented 5 years ago

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

tsotnesharvadze commented 4 years ago

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

namper commented 4 years ago

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

Raynur2000 commented 4 years ago

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

hypy13 commented 3 years ago

still didnt solve it? :(

brunowego commented 3 years ago

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

aryaniyaps commented 3 years ago

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

namper commented 3 years ago

@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 commented 3 years ago

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.

MrAyubi commented 2 years ago

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