Opened 13 years ago

Closed 10 years 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: dev
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 12 years ago.
Failing testcase.
17417__fixed_implementation.patch (1.4 KB ) - added by Hiroki Kiyohara 11 years ago.

Download all attachments as: .zip

Change History (9)

comment:1 by anonymous, 13 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #13223. See also #5681.

comment:2 by Ramiro Morales, 13 years ago

Resolution: duplicate
Status: closedreopened

comment:3 by Ramiro Morales, 13 years ago

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

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.

by Gregor Müllegger, 12 years ago

Failing testcase.

comment:5 by Aymeric Augustin, 12 years ago

Status: reopenednew

by Hiroki Kiyohara, 11 years ago

comment:6 by Hiroki Kiyohara, 11 years ago

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 by Tim Graham, 10 years ago

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