commit b80f5eee048008dc2fb5f964c12fcb6c0b307937 Author: James Turk Date: Thu May 28 00:21:04 2015 -0400 testing an idea diff --git a/fkreplace/__init__.py b/fkreplace/__init__.py new file mode 100644 index 0000000..e38d0c4 --- /dev/null +++ b/fkreplace/__init__.py @@ -0,0 +1,14 @@ +# thanks to https://djangosnippets.org/snippets/2283/ for inspiration on m2m + +def migrate(from_obj, to_obj): + for related in from_obj._meta.get_all_related_objects(): + accessor_name = related.get_accessor_name() + varname = related.field.name + getattr(from_obj, accessor_name).all().update(**{varname: to_obj}) + + for related_m2m in from_obj._meta.get_all_related_many_to_many_objects(): + accessor_name = related.get_accessor_name() + varname = related.field.name + + #for obj in getattr(from_obj, varname) + diff --git a/runtests.py b/runtests.py new file mode 100755 index 0000000..0c2ef30 --- /dev/null +++ b/runtests.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python +import os +import sys + +import django +from django.conf import settings +from django.test.utils import get_runner + +if __name__ == "__main__": + os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings' + django.setup() + TestRunner = get_runner(settings) + test_runner = TestRunner() + failures = test_runner.run_tests(["tests"]) + sys.exit(bool(failures)) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/models.py b/tests/models.py new file mode 100644 index 0000000..bcf7cae --- /dev/null +++ b/tests/models.py @@ -0,0 +1,21 @@ +from django.db import models + + +class Person(models.Model): + name = models.CharField(max_length=100) + friends = models.ManyToManyField('self') + + +class Number(models.Model): + person = models.ForeignKey(Person, related_name='numbers') + number = models.CharField(max_length=10) + + +class SSN(models.Model): + person = models.OneToOneField(Person) + number = models.CharField(max_length=10) + + +class Group(models.Model): + name = models.CharField(max_length=100) + people = models.ManyToManyField(Person) diff --git a/tests/settings.py b/tests/settings.py new file mode 100644 index 0000000..340d6b5 --- /dev/null +++ b/tests/settings.py @@ -0,0 +1,15 @@ +SECRET_KEY = 'so-secret' + +INSTALLED_APPS = ( + 'fkreplace', +) + +MIDDLEWARE_CLASSES = () + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': 'db.sqlite3', + } +} + diff --git a/tests/tests.py b/tests/tests.py new file mode 100644 index 0000000..95e73fb --- /dev/null +++ b/tests/tests.py @@ -0,0 +1,8 @@ +from django.test import TestCase +from .models import Person, Number, SSN + +def setUp(): + a = Person.objects.create(name='alf') + b = Person.objects.create(name='bee') + Number.objects.create(person=a, number='123') + Number.objects.create(person=a, number='1234')