BertrandBordage / django-super-inlines

Adds useful features to inlines, such as the ability to nest them.
BSD 3-Clause "New" or "Revised" License
75 stars 8 forks source link

Validation error #2

Closed 8cylinder closed 9 years ago

8cylinder commented 9 years ago

When saving a form with a third level, and the second level model has a required field but is not filled out, an error is thrown. Validation is being done on the top level model, but not the middle one. This is the exception msg:

Cannot assign "<Server: >": "Server" instance isn't saved in the database.

In this case "Server:" is the name of my second level model. If you want I can give you the entire debug output but for now here is the traceback:

Environment:

Request Method: POST
Request URL: http://127.0.0.1:8000/admin/ui/project/4/

Django Version: 1.8.1
Python Version: 2.7.6
Installed Applications:
('super_inlines',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'ui')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')

Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in wrapper
  616.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  110.                     response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/sites.py" in inner
  233.             return view(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in change_view
  1519.         return self.changeform_view(request, object_id, form_url, extra_context)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapper
  34.             return bound_func(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  110.                     response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in bound_func
  30.                 return func.__get__(self, type(self))(*args2, **kwargs2)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in inner
  145.                     return func(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in changeform_view
  1468.                 self.save_related(request, form, formsets, not add)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in save_related
  1102.             self.save_formset(request, form, formset, change=change)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in save_formset
  1090.         formset.save()
File "/Library/Python/2.7/site-packages/django/forms/models.py" in save
  640.         return self.save_existing_objects(commit) + self.save_new_objects(commit)
File "/Library/Python/2.7/site-packages/django/forms/models.py" in save_new_objects
  771.             self.new_objects.append(self.save_new(form, commit=commit))
File "/Library/Python/2.7/site-packages/django/forms/models.py" in save_new
  897.         setattr(form.instance, self.fk.name, self.instance)
File "/Library/Python/2.7/site-packages/django/db/models/fields/related.py" in __set__
  668.                         (value, self.field.rel.to._meta.object_name)

Exception Type: ValueError at /admin/ui/project/4/
Exception Value: Cannot assign "<Server: >": "Server" instance isn't saved in the database.

Cheers!

BertrandBordage commented 9 years ago

Hello @8cylinder,

Could you paste your admin.py file please? If it’s too big or if you want to keep it private, please send it by mail instead.

8cylinder commented 9 years ago

If you want the model.py I can send that also.
Here is the admin.py:

from django.contrib import admin
import super_inlines.admin as si

from .models import Contact, Project, Server, Database, Website
from django.db import models
import django.forms as df

textarea_css = 'height:50px; width:400px;'

class WebsiteAdmin(si.SuperInlineModelAdmin, admin.StackedInline):
    model = Website
    extra = 0
    fieldsets = [
        ('General information',
         {#'classes': ['collapse'],
          'fields':['url', 'title', 'description', 'note']}),
        ('Admin',
         {'classes': ['collapse'],
          'fields': ['admin_url', 'admin_username', 'admin_password']}),
    ]
    formfield_overrides = {
        models.TextField: {
            'widget': df.Textarea(attrs={'style': textarea_css})
            }
    }

class DatabaseAdmin(si.SuperInlineModelAdmin, admin.StackedInline):
    model = Database
    extra = 0
    formfield_overrides = {
        models.TextField: {
            'widget': df.Textarea(attrs={'style': textarea_css})
            }
    }

class ServerAdmin(si.SuperInlineModelAdmin, admin.StackedInline):
    model = Server
    inlines = (DatabaseAdmin, WebsiteAdmin,)
    extra = 0
    fieldsets = [
        (None,
         {'fields':['title', 'description', 'type', 'note']}),
        ('Upload method',
         {#'classes': ['collapse'],
          'fields':['access_method', 'access_url', 'username',
                    'password']}),
        ('Control panel',
         {#'classes': ['collapse'],
          'fields':['control_panel_url', 'control_panel_username',
                    'control_panel_password', 'control_panel_note']}),
        ('SSH',
         {#'classes': ['collapse'],
          'fields':['ssh_url', 'ssh_username', 'ssh_password',
                    'ssh_note']}),
        ('FTP',
         {#'classes': ['collapse'],
          'fields':['ftp_url', 'ftp_username', 'ftp_password',
                    'ftp_client', 'ftp_note']}),
    ]
    formfield_overrides = {
        models.TextField: {
            'widget': df.Textarea(attrs={'style': textarea_css})
            }
    }

class ProjectAdmin(si.SuperModelAdmin):
    model = Project
    inlines = (ServerAdmin,)
    list_display = ('title', 'status', 'local_root')
    fieldsets = [
        (None,
         {'fields':['title', 'status', 'note', 'local_root',
                    'contacts',]}),
        ('Git information',
         {#'classes': ['collapse'],
          'fields': ['git_hosting_url', 'git_url', 'git_username',
                     'git_password']})
    ]
    formfield_overrides = {
        models.TextField: {
            'widget': df.Textarea(attrs={'style': textarea_css})
            }
    }

class ContactAdmin(admin.ModelAdmin):
    list_display = ('name', 'email', 'phone')
    formfield_overrides = {
        models.TextField: {
            'widget': df.Textarea(attrs={'style': textarea_css})
            }
    }

admin.site.register(Project, ProjectAdmin)
admin.site.register(Contact, ContactAdmin)
BertrandBordage commented 9 years ago

Admin configuration seems correct. However, I can’t reproduce it with my own models, either using Django 1.7.8, 1.8.2, with and without django-grappelli.

Can you also share your models.py? To know which relations these inlines are relying on. I have not tested with GenericForeignKey, so that may be it.

8cylinder commented 9 years ago

I just put the project on GitHub. Its in a pretty rough state but you can see the models there.

Heres the project: https://github.com/8cylinder/sink And here is the model: https://github.com/8cylinder/sink/blob/master/ui/models.py

BertrandBordage commented 9 years ago

Thanks for sharing the whole project! I ran it and could reproduce the bug. I’m working on it.

BertrandBordage commented 9 years ago

Fixed by c494168 and pushed to PyPI as 0.1.4!

8cylinder commented 9 years ago

Thanks, that's awesome!