Opened 9 years ago

Closed 9 years ago

#25587 closed Bug (duplicate)

Django fails, when trying to save object "as new" with inlines objects and form validation errors.

Reported by: user0007 Owned by: nobody
Component: contrib.admin Version: 1.8
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Let's assume that I have one existing object in Django Admin with some related objects (inlines). When I try to create a clone (by clicking on "Save as new"), but there are some validation errors, Django will fail, after clicking "Save and continue editing".

Everything works without inlines objects.

Error:

django.db.models.fields in get_prep_value
ValueError: invalid literal for int() with base 10: ''

Stacktrace (most recent call last):

  File "django/core/handlers/base.py", line 113, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "django/contrib/admin/options.py", line 406, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "django/utils/decorators.py", line 91, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "django/views/decorators/cache.py", line 89, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "django/contrib/admin/sites.py", line 202, in inner
    return view(request, *args, **kwargs)
  File "django/utils/decorators.py", line 25, in _wrapper
    return bound_func(*args, **kwargs)
  File "django/utils/decorators.py", line 91, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "django/utils/decorators.py", line 21, in bound_func
    return func(self, *args2, **kwargs2)
  File "django/db/transaction.py", line 223, in inner
    return func(*args, **kwargs)
  File "django/contrib/admin/options.py", line 1038, in add_view
    prefix=prefix, queryset=inline.queryset(request))
  File "www/forms.py", line 566, in __init__
    super(UczestnikAdminInlineFormset, self).__init__(*args, **kwargs)
  File "django/forms/models.py", line 720, in __init__
    queryset=qs, **kwargs)
  File "django/forms/models.py", line 441, in __init__
    super(BaseModelFormSet, self).__init__(**defaults)
  File "django/forms/formsets.py", line 56, in __init__
    self._construct_forms()
  File "django/forms/formsets.py", line 124, in _construct_forms
    self.forms.append(self._construct_form(i))
  File "django/forms/models.py", line 729, in _construct_form
    form = super(BaseInlineFormSet, self)._construct_form(i, **kwargs)
  File "django/forms/models.py", line 463, in _construct_form
    connection=connections[self.get_queryset().db])
  File "django/db/models/fields/__init__.py", line 340, in get_db_prep_lookup
    value = self.get_prep_lookup(lookup_type, value)
  File "django/db/models/fields/__init__.py", line 322, in get_prep_lookup
    return self.get_prep_value(value)
  File "django/db/models/fields/__init__.py", line 555, in get_prep_value
    return int(value)

Change History (6)

comment:1 by user0007, 9 years ago

Version: 1.71.8

comment:2 by Simon Charette, 9 years ago

Do you happen to have any file or image fields? This looks similar to #14760.

in reply to:  2 comment:3 by user0007, 9 years ago

Replying to charettes:

Do you happen to have any file or image fields? This looks similar to #14760.

Nope, but there is a couple of foreign keys.

comment:4 by Tim Graham, 9 years ago

Could you please provide a sample project or a minimal set of models to reproduce the issue?

in reply to:  4 comment:5 by user0007, 9 years ago

Replying to timgraham:

Could you please provide a sample project or a minimal set of models to reproduce the issue?

Sure.

models.py:

class User(models.Model):
    first_name = models.CharField(max_length=255)


class Project(models.Model):
    user = models.ForeignKey(User)
    name = models.CharField(max_length=255)

class Collaborator(models.Model):
    email = models.Email()
    user = models.ForeignKey(User)
    project = models.ForeignKey(Project)

admin.py:

class CollaboratorAdmin(admin.TabularInline):
    model = Collaborator
    extra = 1
    raw_id_fields = ('user',)

class ProjectAdmin(admin.ModelAdmin):
    inlines = [CollaboratorAdmin]
    raw_id_fields = ('user',)
    save_as = True

admin.site.register(Project, ProjectAdmin)
admin.site.register(User)

Follow those steps:

  1. Add Project object with 2 inlines objects.
  2. Go to edit form.
  3. Clear project's "name" input and click "Save as new" button.
  4. Click "Save and continue editing".
  5. Booom :-)

Tested on Django 1.6, 1.7, 1.8

Version 0, edited 9 years ago by user0007 (next)

comment:6 by Tim Graham, 9 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #23387, fixed in Django 1.9.

Note: See TracTickets for help on using tickets.
Back to Top