Duplicate model instance
See original GitHub issueIs your feature request related to a problem? Please describe.
Usually in django to duplicate a model and to not copy every field there is a work around that is pretty straightforward and easy to do. This can be done via setting the pk
to None
. But with tortoise is not that easy, setting also the attribute ._saved_in_db
is necessary.
Describe the solution you’d like Not to access a private variables for this trick to work.
Describe alternatives you’ve considered Maybe a copy method as well, that would be much more intuitive.
Additional context Example of the code needed as of now
book = Book(title='1984', author='George Orwell')
print(book.pk)
await book.save()
print(book.pk)
book.pk = None
book._saved_in_db = False
book.title = 'Animal farm'
await book.save()
print(book.pk)
print(await Book.all().count())
Example of the code that I would like to use instead
book = Book(title='1984', author='George Orwell')
print(book.pk)
await book.save()
print(book.pk)
book.pk = None
book.title = 'Animal farm'
await book.save()
print(book.pk)
print(await Book.all().count())
Issue Analytics
- State:
- Created 3 years ago
- Comments:18 (7 by maintainers)
Top Results From Across the Web
How do I clone a Django model instance object and save it ...
Just change the primary key of your object and run save(). obj = Foo.objects.get(pk=<some_existing_pk>) obj.pk = None obj.save().
Read more >2. How to copy or clone an existing model object?
There is no built-in method for copying model instances, it is possible to create new instance with all fields values copied. If an...
Read more >The Smart Way to Clone Django Instances
We gonna look into cloning Django model instances, applicability of Iterator and Visitor patterns and a little bit into Django models ...
Read more >django-clone
Create copies of a model instance with explicit control on how the instance should be duplicated (limiting fields or related objects copied) with...
Read more >How to Copy Django Model Instance Objects
Create a model · Make an html file · Define a function. Create an object of model; Copy the model instance · Provide...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Released 0.16.13 with clone and implicit change 😄
Normally we only bump the major release if it is a large change that people need to be aware of. And yes, I agree that 0.16 had by now too many releases 🤷
I originally planned 0.17 to include a field refactor so we can support oracle syntax without too many hacks. And so we can differentiate between preparing/escaping data for parameter-passing or inline-SQL or for LIKE. e.g. JSON for Postgresql needs to be a string, but we can’t treat it as a string when it’s inline, because it’s supported natively, it must NOT be quoted like a string.
Also, turns out that Tortoise doesn’t support nullable PK’s for a while now. We generate an error, so my previous case is actually a lot simpler to describe. So the val=None could work for cases where we can generate the pk, and it is now much simpler a condition.
pk=None
can safely imply_saved_in_db=False
.