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: | 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)
Change History (9)
comment:1 by , 13 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
comment:2 by , 13 years ago
Resolution: | duplicate |
---|---|
Status: | closed → reopened |
comment:3 by , 13 years ago
Resolution: | → worksforme |
---|---|
Status: | reopened → 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 by , 13 years ago
Patch needs improvement: | set |
---|---|
Resolution: | worksforme |
Status: | closed → reopened |
Summary: | "Save as new" is buggy when a ValidationError is raised → "Save as new" does not work when a ValidationError is raised |
Triage Stage: | Unreviewed → Accepted |
Version: | 1.3 → 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.
comment:5 by , 12 years ago
Status: | reopened → new |
---|
by , 11 years ago
Attachment: | 17417__fixed_implementation.patch added |
---|
comment:6 by , 11 years ago
Cc: | 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 , 10 years ago
Resolution: | → duplicate |
---|---|
Status: | new → 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.
Duplicate of #13223. See also #5681.