Opened 4 years ago

Closed 2 months ago

#17417 closed Bug (duplicate)

"Save as new" does not work when a ValidationError is raised

Reported by: cauley.chris@… Owned by: nobody
Component: contrib.admin Version: master
Severity: Normal Keywords: save_as unique
Cc: hirokiky@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

We use save_as = True for several of our models on a news site I maintain. Each model has a unique "slug" field. If you press "Save as New" without changing the slug field a ValidationError is raised, and the for is displayed to the user with a warning (as it should). However, the url is that of the old entity, the "Save as New" button is replaced with "Save and Add Another" and all three buttons raise an ValueError (trying to int() an empty string, presumably the id).

Attachments (2)

17417__failing_test_case.patch (920 bytes) - added by gregmuellegger 3 years ago.
Failing testcase.
17417__fixed_implementation.patch (1.4 KB) - added by hirokiky 2 years ago.

Download all attachments as: .zip

Change History (9)

comment:1 Changed 4 years ago by anonymous

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #13223. See also #5681.

comment:2 Changed 4 years ago by ramiro

  • Resolution duplicate deleted
  • Status changed from closed to reopened

comment:3 Changed 4 years ago by ramiro

  • Resolution set to worksforme
  • Status changed from reopened to closed

(sorry, anonymous user above was me)

Wait, the OP doesn't mention inlines so this isn't a duplicate of #13223.

But I can't reproduce the reported error with this simple model

class Article(models.Model):
    title = models.CharField(max_length=30)
    slug = models.CharField(max_length=200, unique=True)

    def __unicode__(self):
        return self.title

and ModelAdmin

class ArticleAdmin(admin.ModelAdmin):
    save_as = True


admin.site.register(Article, ArticleAdmin)

(tested this with 1.3 and trunk)

comment:4 Changed 3 years ago by gregmuellegger

  • Patch needs improvement set
  • Resolution worksforme deleted
  • Status changed from closed to reopened
  • Summary changed from "Save as new" is buggy when a ValidationError is raised to "Save as new" does not work when a ValidationError is raised
  • Triage Stage changed from Unreviewed to Accepted
  • Version changed from 1.3 to master

This bug is valid, and I have a proof for that :) (see attached testcase).

The form is not showing the "save as new" button after a form validation occured. The reason is pretty simple:

The "save as new" button is only shown on "change_view" pages (which makes sense), see https://github.com/django/django/blob/master/django/contrib/admin/templatetags/admin_modify.py#L32

… however here …
https://github.com/django/django/blob/master/django/contrib/admin/options.py#L1025
… is the "add_view" called when the object is saved, using the "save as new" button.

Changed 3 years ago by gregmuellegger

Failing testcase.

comment:5 Changed 2 years ago by aaugustin

  • Status changed from reopened to new

Changed 2 years ago by hirokiky

comment:6 Changed 2 years ago by hirokiky

  • Cc hirokiky@… added

I added a patch to fix this bug.

It changes to call add_view when the form is valid and "_saveasnew" in request.POST.
The form is not valid, it operates as same the normal "save" behavior.

The gregmuellegger's test patch can't be used as it is.
What do you say about this behavior?

comment:7 Changed 2 months ago by timgraham

  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #23387 which has been fixed in master (1.9) by only showing the "Save as new" button if it's selected and there are validation errors.

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