tortoise-orm: Duplicate model instance
Is 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())
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 18 (7 by maintainers)
Commits related to this issue
- Easier cloning of records(#417) — committed to tortoise/tortoise-orm by grigi 4 years ago
Released 0.16.13 with clone and implicit change 😄