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

AuditLog error when cloning objects #664

Open
raayu83 opened this issue Aug 9, 2024 · 5 comments
Open

AuditLog error when cloning objects #664

raayu83 opened this issue Aug 9, 2024 · 5 comments

Comments

@raayu83
Copy link

raayu83 commented Aug 9, 2024

I'm trying to clone objects including their m2m fields using a solution I found on StackOverflow.

Unfortunately I get the following error:
null value in column "object_pk" of relation "auditlog_logentry" violates not-null constraint

I assume this is because the cloned objects pk field has to be set to None in order for it to be assigned with a new ID automatically.

Is there any way to work around this?

@jcgiuffrida
Copy link

You can get around this by manually assigning an id to the copied object but be very careful it's not an ID already in use, or you'll overwrite something else. You could also probably temporarily unregister the model with AuditLog, which disables the signals.

@raayu83
Copy link
Author

raayu83 commented Aug 26, 2024

Since these seemed more like hacks than an actual solution I switched to django-simple-history which suits my usecase better

@imanerahmaoui
Copy link

Hello, I am encountering the same issue. Did you manage to find a solution? I prefer not to use an alternative and would like to resolve the problem directly. I would be grateful for your help. Thank you in advance!

@hramezani
Copy link
Member

Hello, I am encountering the same issue. Did you manage to find a solution? I prefer not to use an alternative and would like to resolve the problem directly. I would be grateful for your help. Thank you in advance!

Not yet! Unfortunately, I don't have time to work on it. It would be great if you can work on the issue and prepare a fix

@imanerahmaoui
Copy link

Hello, I am encountering the same issue. Did you manage to find a solution? I prefer not to use an alternative and would like to resolve the problem directly. I would be grateful for your help. Thank you in advance!

Not yet! Unfortunately, I don't have time to work on it. It would be great if you can work on the issue and prepare a fix

Hello again, i finally did fix the problem, you will find bellow an example how I handled it in my project :

baseobj = ObjectModel.objects.get(pk=12)
obj = baseobj
obj.pk = None 
obj.save()

instead of following the previous method, i do it otherwise now :

baseobj = ObjectModel.objects.get(pk=12)
obj = ObjectModel()
for field in baseobj._meta.fields:
                                if field.name != 'id':  
                                    setattr(obj, field.name, getattr(baseobj, field.name))

Hope it helps

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

No branches or pull requests

4 participants