Snippet List
I've got a bunch of `Models` that form a tree like structure. I'd like to duplicate them all changing one field to something else.
Say for example I've got a `Website` which has `Pages` and `Links` and all kinds of other `Models`. Each one of these belong to a `User` (through a foreign key relation). I could use `duplicate` to create a copy of an entire website and give it to another `User` with something like this:
class Website(Model):
owner = ForeignKey('auth.User')
...
class Link(Model):
owner = ForeignKey('auth.User')
...
class Page(Model):
owner = ForeignKey('auth.User')
...
##################################
website = Website.objects.get(pk=1)
new_owner = User.objects.get(pk=1)
duplicate(website, new_owner, 'owner')
For a in depth example of the problem see: [Duplicating Model Instances @ STO](http://stackoverflow.com/questions/437166/duplicating-model-instances-and-their-related-objects-in-django-algorithm-for-r)
*Note*
* Not tested with anything but simple Foreign Key relations - the model ordering is _very_ naive.
- copy
- duplicate
- denormalize
Add this as a superclass of any Django model to allow making copies of instances of that model:
class Entry(models.Model, CloneableMixin):
[...]
e = Entry.objects.get(...)
e_clone = e.clone()
e_clone.title = 'Cloned Entry'
e.save()
The new object is saved during the clone process and ManyToMany relations are copied as well.
- model
- mixin
- copy
- clone
- clonable
Create a copy of a model instance.
Works in model inheritance case where ``instance.pk = None`` is
not good enough, since the subclass instance refers to the
parent_link's primary key during save.
M2M relationships are currently not handled, i.e. they are not
copied.
See also Django #4027.
A django admin command that takes a fixture and makes the target database the same as that fixture, deleting objects that in the database but not in the fixture, updating objects that are different in the database, and inserting missing ones.
Place this code in your_app/management/commands/syncdata.py
You will need to use manage.py (not django-admin.py) for Django to recognise custom commands (see http://www.djangoproject.com/documentation/django-admin/#customized-actions).
This snippet is the 'loaddata' command with this patch applied: http://code.djangoproject.com/ticket/7159 (with minor tweaks).
The intention is that 'dumpdata' on system A followed by 'syncdata' on system B is equivalent to a database copy from A to B. The database structure in A and B must match.
- admin
- database
- import
- commands
- copy
- command
- synchronise
- publish
Generator to help make newforms classes a bit like inspectdb does for generating rough model code. This is a standalone script to go in your project directory with settings.py and called from the prompt. It looks up the model, and generates code to copy/paste into your app. Hopefully to make a building a custom form with a lot of fields a little easier. Every argument should be a model identifier of form appname.modelname.
Usage from the command line:
python form_gen myapp.MyModel myapp.MyOtherModel
- form
- copy
- generator
- paste
Somebody mentioned in #django the other day that they have multiple databases with the same schema... Well *lucky me* so do I!!
This is one way to work with this issue. I've also included migrate_table_structure just in case the schema doesn't exist in the new database yet.
As per the multiple-db-support branch, write all of your databases into the OTHER_DATABASES in settings.py. You can now copy models from your various models.py files and use them in different databases at the same time.
This can also be used to migrate databases from one dialect to the other without having to translate your data into an interim format (e.g. csv, XML). You can just do:
qs = MyModel.objects.filter(**filters)
NewModel = duplicate_model_and_rels(MyModel, 'new_db')
#Assuming that the schema is already in new_db:
for mod in qs:
new = NewModel()
new.__dict__ = mod.__dict__
new.save()
I tried this using some hacks with SQLAlchemy, and the above approach is a huge amount quicker! I've used this to copy some stuff from an oracle db, into a sqlite db so i could carry on working later and transferred about 20,000 in 5 mins or so.
GOTCHAS
========
This only works against my copy of multi-db as I've made a couple of changes. My copy is substantially the same as my patch attached to ticket 4747 though, so it might work to a point (especially the data migration aspect). If it doesn't work hit me up and I'll send you my patch against trunk.
I'm not too crazy about the code in copy_field, it works fine, but looks ugly... If anyone knows of a better way to achieve the same, please let me know.
6 snippets posted so far.