| 1789 | def test_run_python_caching(self): |
| 1790 | def create_old_man(models, schema_editor): |
| 1791 | Author = models.get_model("test_authors", "Author") |
| 1792 | Book = models.get_model("test_books", "Book") |
| 1793 | author = Author.objects.create(name="Hemingway") |
| 1794 | Book.objects.create(title="Old Man and The Sea", author=author) |
| 1795 | |
| 1796 | for book in Book.objects.all(): |
| 1797 | book.new_title = book.title |
| 1798 | book.save() |
| 1799 | |
| 1800 | def test_prefetch(models, schema_editor): |
| 1801 | Author = models.get_model("test_authors", "Author") |
| 1802 | authors = Author.objects.all().prefetch_related('book_set') |
| 1803 | assert authors[0].book_set.all()[0].new_title == "Old Man and The Sea" |
| 1804 | |
| 1805 | create_author = migrations.CreateModel( |
| 1806 | "Author", |
| 1807 | [ |
| 1808 | ("id", models.AutoField(primary_key=True)), |
| 1809 | ("name", models.CharField(max_length=100)), |
| 1810 | ], |
| 1811 | options={}, |
| 1812 | ) |
| 1813 | create_book = migrations.CreateModel( |
| 1814 | "Book", |
| 1815 | [ |
| 1816 | ("id", models.AutoField(primary_key=True)), |
| 1817 | ("title", models.CharField(max_length=100)), |
| 1818 | ("new_title", models.CharField(max_length=100)), |
| 1819 | ("author", models.ForeignKey("test_authors.Author")) |
| 1820 | ], |
| 1821 | options={}, |
| 1822 | ) |
| 1823 | create_old_man = migrations.RunPython(create_old_man) |
| 1824 | delete_book_title = migrations.RemoveField("Book", "title") |
| 1825 | test_prefetch = migrations.RunPython(test_prefetch) |
| 1826 | |
| 1827 | project_state = ProjectState() |
| 1828 | new_state = project_state.clone() |
| 1829 | with connection.schema_editor() as editor: |
| 1830 | create_author.state_forwards("test_authors", new_state) |
| 1831 | create_author.database_forwards("test_authors", editor, project_state, new_state) |
| 1832 | project_state = new_state |
| 1833 | new_state = new_state.clone() |
| 1834 | with connection.schema_editor() as editor: |
| 1835 | create_book.state_forwards("test_books", new_state) |
| 1836 | create_book.database_forwards("test_books", editor, project_state, new_state) |
| 1837 | project_state = new_state |
| 1838 | new_state = new_state.clone() |
| 1839 | with connection.schema_editor() as editor: |
| 1840 | create_old_man.state_forwards("test_books", new_state) |
| 1841 | create_old_man.database_forwards("test_books", editor, project_state, new_state) |
| 1842 | with connection.schema_editor() as editor: |
| 1843 | delete_book_title.state_forwards("test_books", new_state) |
| 1844 | delete_book_title.database_forwards("test_books", editor, project_state, new_state) |
| 1845 | project_state = new_state |
| 1846 | new_state = new_state.clone() |
| 1847 | with connection.schema_editor() as editor: |
| 1848 | test_prefetch.state_forwards("test_books", new_state) |
| 1849 | test_prefetch.database_forwards("test_books", editor, project_state, new_state) |
| 1850 | |
| 1851 | |