Django Admin: New instance based on selection using admin actions

Dear forum, I’m learning Django and I playing with actions. I have the following Book library scenario (simplified) :

loanchoices = (
    ('a', 'On Loan'),
    ('b', 'Overdue'),
    ('c', 'Available')
class Book(models.Model):
    title = models.CharField(max_length=200)
    author = models.ForeignKey('Author', on_delete=models.SET_NULL, null=True)
    summary = models.TextField(max_length=1000, help_text="Enter a brief description of the book")
    ID = models.CharField(max_length=200)
class Loans(models.Model):
    borrowername = models.CharField(max_length=200)
    booksborrowed = models.ManyToManyField(Book, help_text="Select the books for borrow")
    loanstatus = models.CharField(max_length=200, choices=loanchoices, default='a')

I’d like to select several books from Django Admin Book Listview and create a new Loans instance based on that selection. That is, an action that redirects to a Loans add form and the booksborrowed field been prepopulated with the previous Book selection.
I tried a lot but I couldn’t get on how to pass the selection to a new instance.
Thanks in advance!

This is the type of situation where an intermediate page would help.

Your action can then redirect you to your page to create a new Loan instance, passing the selected Book instances as a query parameter. (The examples in that section cover this type of situation.)

Thanks Ken for the response. I haven’t success on how to pass a Book instance to booksborrowed since the latter is a manytomany field. I tried with:
where pk is a ‘,’.join of pks of the books selected, but didn’t work

You can’t pass an instance across a redirect. You’re passing the pk of those instances. You need to query for the instances using those pk if you need the instances themselves.

It would help if you posted your action function and the destination view to show what you’ve tried.

It worked! Passing the pk of those instances did it.
Thanks a lot Ken!

Kind regards,