I've found myself in the situation of needing to copy model data to new model instances. This rapidly turned into a twisty sort of hell where I was doing recursion on graphs in order to preserve all the foreign key references (and then special-casing all the OneToOne fields, because naively copying those will throw IntegrityErrors right and left...) Now it is one of those unmaintainable-horror corners of the codebase that no one wants to go near.
Just back up your whole database. If you're using postgres, pg_dump makes this really straightforward. On Sun, Oct 16, 2016 at 2:40 AM, Bernd Wechner <[email protected]> wrote: > A curious question I've had trouble finding an answer for alas. I have a > model that I'd like to backup in the database in a backup model. This being > the pro-forma so to speqk: > > from django.db import models > > class MyModel(models.Model): > # Declare fields .... > > class MyModel_backup(MyModel): > def create(self): > self.objects = MyModel.objects.all() > > But there are two immediate problems. > > 1. Deriving from MyModel reveals itself in the migration to be > generating a model which has a single OneToOne reference to MyModel. That > is ti does not appear to create a duplicate model at all. Which leaves me > wondering how to create a duplicate model without repeating the code. > > 2. I have no really idea how to copy all the objects of MyModel to a > new model. > > I may be approaching it poorly and am open to better ideas. I'm used to > doing it in SQL, essentially having an identically defined backup table, > just copying data to that table before doing a (risky) table wide operation > on the first. > > I could of course export a serialized backup to a disk file, but am > exploring options for keeping one backup in the database itself. > > I'd rather, I admit hear options for doing that than philosophic > appraisals of the benefits of an in-database copy vs, database exports. > > Regards, > > Bernd. > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at https://groups.google.com/group/django-users. > To view this discussion on the web visit https://groups.google.com/d/ > msgid/django-users/a19ba010-572b-4097-a988-f357cc5b6c31%40googlegroups.com > <https://groups.google.com/d/msgid/django-users/a19ba010-572b-4097-a988-f357cc5b6c31%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- Andromeda Yelton Vice President/President-Elect, Library & Information Technology Association: http://www.lita.org http://andromedayelton.com @ThatAndromeda <http://twitter.com/ThatAndromeda> -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFE1XCYW9cLixRSG2bmBM3Knm0hhf_4q%2BcZvuvvXcDPZmf9nKw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.

