Giter Site home page Giter Site logo

django-modelclone's Introduction

django-modelclone

Allows users to duplicate a model in admin.

Installation

$ pip install django-modelclone

then:

  1. Add 'modelclone' to INSTALLED_APPS
  2. In your admin.py files extend from modelclone.ClonableModelAdmin instead of Django's ModelAdmin

The models that have admin configuration extending modelclone.ClonableModelAdmin will have a new link on the Change page to duplicate that object

Screenshot Duplicate link

This links redirects to a page similar to an Add page but with all the fields already filled with the values from the original object.

Note that you still need to save to get a new object. And make sure to edit fields that must be unique otherwise you will get a validation error.

But Django already has a 'save as'

Yes, I know. Django Admin has a save_as feature that adds a new button to your Change page to save a new instance of that object.

I don't like the way this feature works because you will save an identical copy of the original object (if you don't get validation errors) as soon as you click that link, and if you forget to make the small changes that you wanted in the new object you will end up with a duplicate of the existing object.

On the other hand, django-modelclone offers an intermediate view, that basically pre-fills the form for you. So you can modify and then save a new instance. Or just go away without side effects.

Requirements

  • Python 2.7 or 3.7
  • Django 1.8, 1.9, 1.10, 1.11, 2.0 or 2.1

See tox.ini

Hacking

Fork the repository on github, make your changes (don't forget the tests) and send a pull request.

To run the tests, install and run Tox:

$ pip install tox
$ tox

You can also run the sample project to test manually. In this case you'll need to install Django, or just use one of the virtualenvs tox creates, for example:

$ source .tox/py27-django15/bin/activate

then start the server

(py27-django15) $ ./manager serve

The app is available on http://localhost:8000/admin/, username and password "admin".

django-modelclone's People

Contributors

applecat avatar axeliodiaz avatar dnmellen avatar igorsobreira avatar j0hnsmith avatar jeanmask avatar kevin1024 avatar leibowitz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

django-modelclone's Issues

Remove Delete link from Clone page

The object doesn't exist, should not have a Delete link.

Another option is to make the Delete link go to the original object Delete page, but I don't think it makes sense

Request: clone DjangoCMS Placeholder content

I'm using django-modelclone on several projects. It's great!

However... :-)

If I apply it to a model using DjangoCMS's Placeholder field, it dutifully clones the model instance with the Placeholder field. However, it creates an empty placeholder. This would be OK, EXCEPT that my stakeholders complain that they have to re-create (most of) the original page's content within the Placeholder field.

What would be ideal is to ALSO recusively clone the plugin instances (or I suppose to optionally do so). DjangoCMS ties plugin instances to the Placeholder field (and some plugins can have child plugins so it's not conveniently straightforward) so (unfortunately) you have to create additional records in those tables if you clone the original page.

(I realize that this is probably out of scope for this package, since not everyone using it also uses DjangoCMS, and frankly, Wagtail does a better job at storing the custom content metadata, but this is the real-world situation I'm in. I suppose I could write my own cloning function, but that would mean mostly re-inventing the wheel just to get the enhanced functionality. But perhaps there's something in the existing package I'm missing.)

update modelclone with django 4.0 compatible

In django 4.0 some functions has been removed permanently. One of the major function is removed that must effect in this package (django-modelclone). that function is URL which was present in from django.conf.urls import url.

Here my suggestion is to replace url() with re_path() function.
re_path present in from django.urls import re_path

Problem to duplicate image inline

Hi, I have some images in inline in my admin form and when I use Duplicate button it does not copy any images. Do you have any solution for this?

Provide notice for duplicate fields in clone view

Add some kind of notice that shows up next to all unique fields when editing a cloned object. This should not look like the error message that happens when you try to save. More of warning instead of an error.

Says something like "Please change this {{ label}} before saving"

Document how relationships are supported

i.e. inlines, m2m.

It was my first thought and there's no mention of it in the readme. I'm guessing it's either:

  1. Not at all
  2. So flawlessly we didn't see a need to mention it.

;-)

RemovedInDjango110Warning: django.conf.urls.patterns() is deprecated

Hello,

I got a lot of these deprecation warning log messages - that complicates log analysis and the whole life, even with "django-admin --help".
Could you please update ClonableModelAdmin.get_urls() to avoid using patterns() when in Django 1.9, eg:
new_urlpatterns = [ url(r'^(.+)/clone/$', self.admin_site.admin_view(self.clone_view), name=url_name), ]
Sorry for not providing pull request - I'm first time here and know nothing about test framework.

Thank you.
-Ilya

2016-03-16 20:58:20,917 init - WARNING 10632 140559809300224 /opt/py3.5_venv/lib/python3.5/site-packages/modelclone/admin.py:47: RemovedInDjango110Warning: django.conf.urls.patterns() is deprecated and will be removed in Django 1.10. Update your urlpatterns to be a list of django.conf.urls.url() instances instead.
name=url_name)

.....

(py3.5_venv) [root@qa-tools-dashboard py3.5_venv]# pip3.5 -vv list | grep -e Dja -e model
Django (1.9.4)
django-modelclone (0.5.3)

OBJECT needs to have a value for field "FIELD" before this many-to-many relationship can be used error

Hi there,

I'm trying to use modelclone to duplicate an object where two fields are required and need to be unique. I'm using the exact setup explained in the readme and am able to get a prepopulated modelform. Without changing anything and trying to save, I get the correct validation response, where it denies the save because the data is exactly the same as a database object. If I change the two fields that are required for the change I get the following error:

""<Pattern: 1,2,3-benzotriazole_NEWTEST>" needs to have a value for field "pattern" before this many-to-many relationship can be used." and the traceback points to:

PATH/python2.7/site-packages/modelclone/admin.py in clone_view
ModelForm = self.get_form(request)
formsets = []
if request.method == 'POST':
form = ModelForm(request.POST, request.FILES)
if form.is_valid():
new_object = self.save_form(request, form, change=False) ...
form_validated = True
else:
new_object = self.model()
form_validated = False
prefixes = {}

Where the field "pattern" is a text field. Has anyone experienced this?

Thanks
--Nick

Blank FileField in inlines

I've got "This field is required." on FileField inside inlines while saving clone.
Actually, also I get blank FileField in the main formset of my model.
I use Grappelli, but I've reproduced the same problem in standard admin interface.

RemovedInDjango19Warning from use of get_formsets()

The following warning is being logged after upgrading a project from Django 1.7 to 1.8:

/home/frank/virtualenvs/frank/src/django-modelclone/modelclone/admin.py:95: RemovedInDjango19Warning: ModelAdmin.get_formsets() is deprecated and will be removed in Django 1.9. Use ModelAdmin.get_formsets_with_inlines() instead.
for FormSet, inline in zip(self.get_formsets(request), inline_instances):

Support Django 2.0

As the title says import needs updates to work with django 2.0.
Probably also needs work to support Python 3

FormSet object has no attribute '_construct_forms'

My model has a custom InlineFormset with just one method which is clean().
When I try to use modelclone I get the message:

'DeadlineFormFormSet' object has no attribute '_construct_forms'

I did the following to fix it but it's not ideal:

def _construct_forms(self):
        pass

The duplicate template doesn't have any submit button

When I click on the duplicate button, it leads me to the new form, but this form has no submit button. It only has a 'close' button, that takes me back to the object list.

I am not using any new style. I downloaded it using pip3.

Page not found

Hi there, thanks for the lib!

Well, so the pip is outdated, but I installed the version from master. I guess a page not found error when clicking on the button, here is the url : http://localhost:8000/admin/event/event/4/change/clone/ .
Error message: "event object with primary key u'4/change' does not exist."

Seems like an acute problem, does anyone have the same thing happening?

Add some new maintainers

Hi @RealGeeks @maintainers

This repo is clearly unmaintained for a long time, but lots of people have made PRs and forks, which are a bit difficult to wade through (not being on pypi).

How about adding someone (I'll do it) to be a project admin on pypi, and adding for admin privilege over this repo (or transferring it). We could get the PRs merged, make a new release, bring the CI process up to scratch then submit it to Jazzband allowing the community to maintain it properly whilst staying respectful of the work you did in the first place (forking then making eg django-modelclone-2 on pypi seems crude, it's better to keep OSS projects consolidated if possible).

Would you be keen to do that??

TemplateDoesNotExist at /admin/homepage/homepage/1/change/

I'm having problem getting this to work. I've used this package elsewhere with no problems. What's different is: 1) I'm using Python 3.7 + Django 2.1; 2) the model in question also subclasses another class that alters the default ModelAdmin - but I don't see how either of these should affect it not finding the modelclone/change_form.html.

It appears that Django is not trying to locate the template within site-packages:

Django tried loading these templates, in this order:
Using engine django:

django.template.loaders.app_directories.Loader: /usr/local/lib/python3.7/site-packages/django/contrib/admin/templates/modelclone/change_form.html (Source does not exist)
django.template.loaders.app_directories.Loader: /usr/local/lib/python3.7/site-packages/django/contrib/auth/templates/modelclone/change_form.html (Source does not exist)
django.template.loaders.app_directories.Loader: /usr/local/lib/python3.7/site-packages/django_extensions/templates/modelclone/change_form.html (Source does not exist)

TEMPLATES in the settings.py is:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

and the admin.py's class is:

from django.contrib import admin
import modelclone
from gatekeeper.admin import GatekeeperSerialAdmin
from .models import Homepage

class HomepageAdmin(modelclone.ClonableModelAdmin, GatekeeperSerialAdmin):
    model = Homepage
    
admin.site.register(Homepage, HomepageAdmin)

I could make it work by explicitly adding
'/usr/local/lib/python3.7/site-packages/modelclone/templates' to the DIRS list in TEMPLATES, but that's not a workable solution if the server setup changes.

Actually - that didn't quite work: you can bring up the instance in the Admin and there's a "duplicate" button. When you click it, the duplicate admin comes up but there's no "save" button, just "close".

Any suggestions/ideas or pointing out where I screwed up :-) would be greatly appreciated.

Can't change uploaded file when cloning

This bug was introduced on #7. If a model has a file field and we clone it the same path of the original file will be saved in the new cloned model. But if a new file was chosen it will be ignored.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.