wrufesh wrufesh - 1 month ago 5x
Python Question

Wagtail: How can I copy a page instance in wagtail with all its property and method

I have a CoursePage model in Wagtail site.

class CoursePage(Page):
institute = models.ForeignKey(Institute)

I have a django models ForeignKey field in it named institute

I want to make a copy of its instance programmatically so that the newly created instance institute field can be modified.

I tried django approach of copying model instance, that is:

course = CoursePage.objects.all()[0]
course.pk = None

But it doesnot work out.
It only works with the model inherited with django models.Model. but not with the model inherited with Page


The Page model implements a copy method to do this:

def copy(self, recursive=False, to=None, update_attrs=None,
         copy_revisions=True, keep_live=True, user=None):

The parameters it accepts are:

  • recursive - if true, copies child pages as well
  • to - the page to create the new copy under (defaults to creating a sibling of the existing page)
  • update_attrs - a dict of fields to update while copying, such as {'institute': other_institute}
  • copy_revisions - whether to copy revision history
  • keep_live - whether to copy the 'live' status
  • user - the owner of the new page, for permission purposes