djongo icon indicating copy to clipboard operation
djongo copied to clipboard

Unable to use django admin to save nested embedded model in model array field

Open v1nnyc opened this issue 8 years ago • 15 comments

One line description of the issue

Unable to use django admin to save nested embedded model in model array field

Python script

`from .user import Project
from django import forms
from models import *
from djongo.models import ArrayModelField

import uuid
from djongo import models

# To enable FileField to store static files
from django.core.files.storage import FileSystemStorage
from django.conf import settings

class SSH(models.Model):
    SSH_name = models.CharField(max_length = 50, null=True, blank=True)
    value_max = models.FloatField(null=True, blank=True)
    value_nominal = models.FloatField(null=True, blank=True)
    value_min = models.FloatField(null=True, blank=True)
    unit = models.CharField(max_length = 50, null=True, blank=True)
    is_range = models.BooleanField(help_text="Is this variable a range?")

    class Meta:
        abstract = True

class SSHForm(forms.ModelForm):
    class Meta:
        model = SSH
        fields = '__all__'

class Space(models.Model):
    Space_TYPE = (
        ("BOOL", "Boolean"),
        ("FLT","Float"),
        ("CHAR", "String" ),)

    label = models.CharField(max_length = 50, null=True, blank=True)
    SSH_array = ArrayModelField(model_container=SSH, model_form_class=SSHForm, default = [] , blank = True)
    Space_type = models.CharField(max_length=20, choices = Space_TYPE, null=True, blank=True)

    class Meta:
        abstract = True


class SpaceForm(forms.ModelForm):
    class Meta:
        model = Space
        fields = '__all__'

class CatPart(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, blank = False, null = False)
    name = models.CharField(max_length = 50, null=True, blank=True)
    vendor = models.CharField(max_length = 50, null=True, blank=True)
    category = models.CharField(max_length = 50, null=True, blank=True)
    description = models.TextField(max_length = 50, null=True, blank=True)
    image = models.ImageField(upload_to="images", default="images/no_image_available.png", blank=True)
    spec_sheet = models.ImageField(upload_to="images", default="images/no_image_available.png", blank=True)
    Space_array = ArrayModelField(model_container=Space, model_form_class=SpaceForm, default = [] , blank = True)
    data_interface_array = ArrayModelField(model_container=DataInterface, model_form_class=DataInterfaceForm, default = [] , blank = True)
    power_interface_array = ArrayModelField(model_container=PowerInterface, model_form_class=PowerInterfaceForm, default = [] , blank = True)
    rf_interface_array = ArrayModelField(model_container=RfInterface, model_form_class=RfInterfaceForm, default = [], blank = True)

    def __str__(self):
        return self.name
`

Traceback

`Environment:

Request Method: POST Request URL: http://127.0.0.1:8000/admin/pizza/catpart/add/

Django Version: 2.0.1 Python Version: 3.6.5 Installed Applications: ['pizza.apps.pizzaConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', 'rest_framework.authtoken', 'corsheaders', 'rest_auth', 'django.contrib.sites', 'allauth', 'allauth.account', 'allauth.socialaccount', 'rest_auth.registration', 'drf_generators'] Installed Middleware: ['django.middleware.security.SecurityMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'corsheaders.middleware.CorsMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware']

Template error: In template /pizza/lib/python3.6/site-packages/django/contrib/admin/templates/admin/includes/fieldset.html, error at line 19 ManagementForm data is missing or has been tampered with 9 : {% for field in line %} 10 : <div{% if not line.fields|length_is:'1' %} class="field-box{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% if not field.is_readonly and field.errors %} errors{% endif %}{% if field.field.is_hidden %} hidden{% endif %}"{% elif field.is_checkbox %} class="checkbox-row"{% endif %}> 11 : {% if not line.fields|length_is:'1' and not field.is_readonly %}{{ field.errors }}{% endif %} 12 : {% if field.is_checkbox %} 13 : {{ field.field }}{{ field.label_tag }} 14 : {% else %} 15 : {{ field.label_tag }} 16 : {% if field.is_readonly %} 17 :

{{ field.contents }}
18 : {% else %} 19 : {{ field.field }} 20 : {% endif %} 21 : {% endif %} 22 : {% if field.field.help_text %} 23 :
{{ field.field.help_text|safe }}
24 : {% endif %} 25 :
26 : {% endfor %} 27 :
28 : {% endfor %} 29 :

Traceback:

File "/pizza/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner 35. response = get_response(request)

File "/pizza/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response 158. response = self.process_exception_by_middleware(e, request)

File "/pizza/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response 156. response = response.render()

File "/pizza/lib/python3.6/site-packages/django/template/response.py" in render 106. self.content = self.rendered_content

File "/pizza/lib/python3.6/site-packages/django/template/response.py" in rendered_content 83. content = template.render(context, self._request)

File "/pizza/lib/python3.6/site-packages/django/template/backends/django.py" in render 61. return self.template.render(context)

File "/pizza/lib/python3.6/site-packages/django/template/base.py" in render 175. return self._render(context)

File "/pizza/lib/python3.6/site-packages/django/template/base.py" in _render 167. return self.nodelist.render(context)

File "/pizza/lib/python3.6/site-packages/django/template/base.py" in render 943. bit = node.render_annotated(context)

File "/pizza/lib/python3.6/site-packages/django/template/base.py" in render_annotated 910. return self.render(context)

File "/pizza/lib/python3.6/site-packages/django/template/loader_tags.py" in render 155. return compiled_parent._render(context)

File "/pizza/lib/python3.6/site-packages/django/template/base.py" in _render 167. return self.nodelist.render(context)

File "/pizza/lib/python3.6/site-packages/django/template/base.py" in render 943. bit = node.render_annotated(context)

File "/pizza/lib/python3.6/site-packages/django/template/base.py" in render_annotated 910. return self.render(context)

File "/pizza/lib/python3.6/site-packages/django/template/loader_tags.py" in render 155. return compiled_parent._render(context)

File "/pizza/lib/python3.6/site-packages/django/template/base.py" in _render 167. return self.nodelist.render(context)

File "/pizza/lib/python3.6/site-packages/django/template/base.py" in render 943. bit = node.render_annotated(context)

File "/pizza/lib/python3.6/site-packages/django/template/base.py" in render_annotated 910. return self.render(context)

File "/pizza/lib/python3.6/site-packages/django/template/loader_tags.py" in render 67. result = block.nodelist.render(context)

File "/pizza/lib/python3.6/site-packages/django/template/base.py" in render 943. bit = node.render_annotated(context)

File "/pizza/lib/python3.6/site-packages/django/template/base.py" in render_annotated 910. return self.render(context)

File "/pizza/lib/python3.6/site-packages/django/template/loader_tags.py" in render 67. result = block.nodelist.render(context)

File "/pizza/lib/python3.6/site-packages/django/template/base.py" in render 943. bit = node.render_annotated(context)

File "/pizza/lib/python3.6/site-packages/django/template/base.py" in render_annotated 910. return self.render(context)

File "/pizza/lib/python3.6/site-packages/django/template/defaulttags.py" in render 211. nodelist.append(node.render_annotated(context))

File "/pizza/lib/python3.6/site-packages/django/template/base.py" in render_annotated 910. return self.render(context)

File "/pizza/lib/python3.6/site-packages/django/template/loader_tags.py" in render 194. return template.render(context)

File "/pizza/lib/python3.6/site-packages/django/template/base.py" in render 177. return self._render(context)

File "/pizza/lib/python3.6/site-packages/django/template/base.py" in _render 167. return self.nodelist.render(context)

File "/pizza/lib/python3.6/site-packages/django/template/base.py" in render 943. bit = node.render_annotated(context)

File "/pizza/lib/python3.6/site-packages/django/template/base.py" in render_annotated 910. return self.render(context)

File "/pizza/lib/python3.6/site-packages/django/template/defaulttags.py" in render 211. nodelist.append(node.render_annotated(context))

File "/pizza/lib/python3.6/site-packages/django/template/base.py" in render_annotated 910. return self.render(context)

File "/pizza/lib/python3.6/site-packages/django/template/defaulttags.py" in render 211. nodelist.append(node.render_annotated(context))

File "/pizza/lib/python3.6/site-packages/django/template/base.py" in render_annotated 910. return self.render(context)

File "/pizza/lib/python3.6/site-packages/django/template/defaulttags.py" in render 314. return nodelist.render(context)

File "/pizza/lib/python3.6/site-packages/django/template/base.py" in render 943. bit = node.render_annotated(context)

File "/pizza/lib/python3.6/site-packages/django/template/base.py" in render_annotated 910. return self.render(context)

File "/pizza/lib/python3.6/site-packages/django/template/defaulttags.py" in render 314. return nodelist.render(context)

File "/pizza/lib/python3.6/site-packages/django/template/base.py" in render 943. bit = node.render_annotated(context)

File "/pizza/lib/python3.6/site-packages/django/template/base.py" in render_annotated 910. return self.render(context)

File "/pizza/lib/python3.6/site-packages/django/template/base.py" in render 999. return render_value_in_context(output, context)

File "/pizza/lib/python3.6/site-packages/django/template/base.py" in render_value_in_context 978. value = str(value)

File "/pizza/lib/python3.6/site-packages/djongo/models/fields.py" in str 351. for form in self.form_set))

File "/pizza/lib/python3.6/site-packages/django/utils/html.py" in format_html_join 118. for args in args_generator))

File "/pizza/lib/python3.6/site-packages/django/utils/html.py" in 117. format_html(format_string, *tuple(args))

File "/pizza/lib/python3.6/site-packages/djongo/models/fields.py" in 351. for form in self.form_set))

File "/pizza/lib/python3.6/site-packages/django/forms/forms.py" in as_table 279. errors_on_separate_row=False)

File "/pizza/lib/python3.6/site-packages/django/forms/forms.py" in _html_output 196. top_errors = self.non_field_errors() # Errors that should be displayed above all fields.

File "/pizza/lib/python3.6/site-packages/django/forms/forms.py" in non_field_errors 305. return self.errors.get(NON_FIELD_ERRORS, self.error_class(error_class='nonfield'))

File "/pizza/lib/python3.6/site-packages/django/forms/forms.py" in errors 174. self.full_clean()

File "/pizza/lib/python3.6/site-packages/django/forms/forms.py" in full_clean 373. if self.empty_permitted and not self.has_changed():

File "/pizza/lib/python3.6/site-packages/django/forms/forms.py" in has_changed 429. return bool(self.changed_data)

File "/pizza/lib/python3.6/site-packages/django/utils/functional.py" in get 36. res = instance.dict[self.name] = self.func(instance)

File "/pizza/lib/python3.6/site-packages/django/forms/forms.py" in changed_data 451. if field.has_changed(initial_value, data_value):

File "/pizza/lib/python3.6/site-packages/djongo/models/fields.py" in has_changed 314. return form_set.has_changed()

File "/pizza/lib/python3.6/site-packages/django/forms/formsets.py" in has_changed 364. return any(form.has_changed() for form in self)

File "/pizza/lib/python3.6/site-packages/django/forms/formsets.py" in iter 68. return iter(self.forms)

File "/pizza/lib/python3.6/site-packages/django/utils/functional.py" in get 36. res = instance.dict[self.name] = self.func(instance)

File "/pizza/lib/python3.6/site-packages/django/forms/formsets.py" in forms 136. for i in range(self.total_form_count())]

File "/pizza/lib/python3.6/site-packages/django/forms/formsets.py" in total_form_count 110. return min(self.management_form.cleaned_data[TOTAL_FORM_COUNT], self.absolute_max)

File "/pizza/lib/python3.6/site-packages/django/utils/functional.py" in get 36. res = instance.dict[self.name] = self.func(instance)

File "/pizza/lib/python3.6/site-packages/django/forms/formsets.py" in management_form 92. code='missing_management_form',

Exception Type: ValidationError at /admin/pizza/catpart/add/ Exception Value: ['ManagementForm data is missing or has been tampered with'] `

v1nnyc avatar Jul 28 '18 00:07 v1nnyc

I am also facing a similar issue @nesdis

FarisHamdi avatar Jul 30 '18 16:07 FarisHamdi

Any chance this gets fixed in the near future? I am having the same problem as well.

renzladia avatar Aug 16 '18 15:08 renzladia

I am facing the same issue as well

jlouis12 avatar Aug 20 '18 20:08 jlouis12

me too

hellonts avatar Aug 21 '18 06:08 hellonts

It happens because file data is ignored in ArrayFormWidget.value_from_datadict and ArrayFormField.clean.

mich83 avatar Sep 01 '18 20:09 mich83

I have the same issue

gggfreak2003 avatar Sep 12 '18 09:09 gggfreak2003

@nesdis Any help for this in near future?

ghost avatar Oct 30 '18 12:10 ghost

I am facing the same issue

mortezadalil avatar Nov 30 '18 16:11 mortezadalil

Same here ...

smn-snkl avatar Jan 03 '19 21:01 smn-snkl

same issue here !

ameenalakhras avatar Jan 24 '19 12:01 ameenalakhras

Same here! Any plans on fixing it? Or anyone who found a work-around?

helena-st avatar Apr 12 '19 08:04 helena-st

I'm also facing the same issue.

saurabh-m523 avatar Nov 11 '19 05:11 saurabh-m523

+1

jiji879 avatar Jan 07 '20 10:01 jiji879

Just in case: in the example given, you are using djongo.models.ArrayModelField, but at least currently, in djongo version 1.3.1, there's not such field. You should be using djongo.models.ArrayField(model_container=).

Leave a comment or reaction so I know this comment was helpful

eldano1995 avatar Mar 31 '20 22:03 eldano1995