Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django admin Add custom button + custom form

Tags:

django

admin

I want to add a custom button near 'Add model_name'. When i click on the newly created button i should like to show a custom form where I cans elect a model out of a select box. When i click on save I want to save this model and chance some parameters so it's a 'add' but without selecting all options again. I give a clear example:

I have a model names 'Book'. The first time i create a new book entry, i have a form 'add Book' and i have to fill out the form completely. So i have the book with primary key = Book_1_1 But now i want to add a second book, it's the same book as the first 1 BUT the version changed, so i want a new book but i don't want to select all items anymore in the standard 'add Book' form, i want something like i click on create new instance --> i can select 1 book out of a select box with all book objects in it, and when i 'save' this a new instance of the book gets generated. This instance has the following primary key: Book_1_2 for example. I know how to save this, but i don't know how to change the admin site to do this. I need 2 things:

1) add a button 'new instance' near 'Add_model_name' 2) Deliver a form with all model_name objects in a select box and when i click save i want to retrieve an object with which i can modify some things to save it as 'new book'.

Any ideas?

UPDATE I already added the 'new' button, but like i can see at this moment instead of the url = add , i have to create a new url inside the admin like add_instance etc. Does someone have any documentation on this?

Regards, Hein

like image 762
Hein Avatar asked Nov 15 '22 06:11

Hein


1 Answers

Making it way too hard on yourself. Just do this:

class MyModelAdmin(admin.ModelAdmin):
    # Other stuff here
    save_as = True

Now you can open up your book entry, change whatever is different and hit "Save as new" and it'll create a new book with that info instead of overwriting the other.

like image 141
Chris Pratt Avatar answered Jan 02 '23 19:01

Chris Pratt