Opened 5 years ago

Closed 16 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 Gregor Müllegger 4 years ago.
Failing testcase.
17417__fixed_implementation.patch (1.4 KB) - added by Hiroki Kiyohara 3 years ago.

Download all attachments as: .zip

Change History (9)

comment:1 Changed 5 years ago by anonymous

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Resolution: duplicate
Status: newclosed

Duplicate of #13223. See also #5681.

comment:2 Changed 5 years ago by Ramiro Morales

Resolution: duplicate
Status: closedreopened

comment:3 Changed 5 years ago by Ramiro Morales

Resolution: worksforme
Status: reopenedclosed

(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 4 years ago by Gregor Müllegger

Patch needs improvement: set
Resolution: worksforme
Status: closedreopened
Summary: "Save as new" is buggy when a ValidationError is raised"Save as new" does not work when a ValidationError is raised
Triage Stage: UnreviewedAccepted
Version: 1.3master

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 4 years ago by Gregor Müllegger

Failing testcase.

comment:5 Changed 4 years ago by Aymeric Augustin

Status: reopenednew

Changed 3 years ago by Hiroki Kiyohara

comment:6 Changed 3 years ago by Hiroki Kiyohara

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 16 months ago by Tim Graham

Resolution: duplicate
Status: newclosed

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