0

To add a simple type of copy functionality to the Django admin I would like to render the default admin add form of a model but with default values. I know that the add form accepts GET parameters to pre-populate most fields, but I don't always want to copy M2M references, sometimes the referenced objects also need to be copied.

What I would like to do in code:

def some_view(self, request, pk):
    modeol_original = FooModel.objects.get(pk)
    model_copy = FooModel()
    copy_attributes_custom(model_original, model_copy)
    return render_default_django_add_page_for_model(model_copy)

As dhana mentioned in the comments, this is possible as seen in: Django set default form values but only for custom templates. I wan't to use the default Django Admin add form/template.

Community
  • 1
  • 1
zeebonk
  • 4,864
  • 4
  • 21
  • 31
  • 1
    May be duplicate http://stackoverflow.com/questions/604266/django-set-default-form-values – dhana May 06 '14 at 08:45
  • The Django admin doesn't easily allow you to copy a model instance. You'll have to create a custom admin view that does that. – schillingt May 07 '14 at 03:30
  • But then how can I render the default add/edit form for an unsaved model instance in that custom view? – zeebonk May 07 '14 at 09:18

1 Answers1

0

Altough I haven't been able to do exactly what I want, I've sort of solved my problem by creating a custom admin action that copies all selected models in a way that is logical for the type of model. By overwriting certain properties and setting a "invalid" status, the user is forced to first edit the copied model before it can be used.

zeebonk
  • 4,864
  • 4
  • 21
  • 31