Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Handle duplicating FileField/ImageField #412

Open
1 task done
jackton1 opened this issue Jul 7, 2021 · 1 comment
Open
1 task done

[Feature] Handle duplicating FileField/ImageField #412

jackton1 opened this issue Jul 7, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@jackton1
Copy link
Member

jackton1 commented Jul 7, 2021

Is your feature request related to a problem? Please describe.

Based on https://groups.google.com/g/django-updates/c/vup1ZShMEgQ?pli=1.

I'll like to get a sense of the requirements of duplicating models with FileField's

Describe the solution you'd like?

Replicating FileFields should optionally be allowed to replicate the source object if required or set else it should be skipped if the current value would point to the same file.

Describe alternatives you've considered?

No response

Anything else?

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@jackton1 jackton1 added the enhancement New feature or request label Jul 7, 2021
@jackton1 jackton1 changed the title [Feature] Handle File fields [Feature] Handle duplicating FileField/ImageField Jul 7, 2021
@jackton1 jackton1 added this to the CloneMixin enhancements milestone Sep 6, 2021
@RomainDLS
Copy link

RomainDLS commented Aug 4, 2023

I have create a workaround for this feature :

from django.db.models.fields.files import FieldFile
from django.core.files.base import ContentFile

from model_clone import CloneMixin as _CloneMixin

class CloneMixin(_CloneMixin):

    _clone_file_fields = []

    @staticmethod
    def _create_copy_of_instance(instance, **kwargs):
        """
        Create copy of instance.
        :param instance: instance to copy
        :param kwargs: kwargs for new instance
        :return: new instance
        """
        new_instance = super()._create_copy_of_instance(instance, **kwargs)

        # Handle file fields
        for f in instance.__class__._meta.concrete_fields:
            file_field = getattr(instance, f.name)
            if (
                f.name in instance._clone_file_fields and
                isinstance(file_field, FieldFile)
            ):
                new_file = ContentFile(file_field.read())
                new_file.name = file_field.name.split('/')[-1]
                setattr(new_instance, f.name, new_file)

        return new_instance

Usage :

class MyModel(models.Model):

    file_object = models.FileField()

    _clone_file_fields = ['file_object']
    _clone_excluded_fields = ['file_object']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants