django-migrations

Django-DB-Migrations: cannot ALTER TABLE because it has pending trigger events

不打扰是莪最后的温柔 提交于 2019-11-28 05:09:36
I want to remove null=True from a TextField: - footer=models.TextField(null=True, blank=True) + footer=models.TextField(blank=True, default='') I created a schema migration: manage.py schemamigration fooapp --auto Since some footer columns contain NULL I get this error if I run the migration: django.db.utils.IntegrityError: column "footer" contains null values I added this to the schema migration: for sender in orm['fooapp.EmailSender'].objects.filter(footer=None): sender.footer='' sender.save() Now I get: django.db.utils.DatabaseError: cannot ALTER TABLE "fooapp_emailsender" because it has

Django data migration when changing a field to ManyToMany

老子叫甜甜 提交于 2019-11-28 03:48:48
I have a Django application in which I want to change a field from a ForeignKey to a ManyToManyField. I want to preserve my old data. What is the simplest/best process to follow for this? If it matters, I use sqlite3 as my database back-end. If my summary of the problem isn't clear, here is an example. Say I have two models: class Author(models.Model): author = models.CharField(max_length=100) class Book(models.Model): author = models.ForeignKey(Author) title = models.CharField(max_length=100) Say I have a lot of data in my database. Now, I want to change the Book model as follows: class Book

Disable migrations when running unit tests in Django 1.7

痴心易碎 提交于 2019-11-28 03:10:39
Django 1.7 introduced database migrations . When running the unit tests in Django 1.7, it forces a migrate , that takes a long time. So I would like to skip the django migrations, and create the database in the final state. I know that ignoring the migrations can be a bad practice, as that part of the code would not be tested. But that's not the case: I'm running the full migrations in the CI test server (jenkins). I only want to skip the migrations in my local tests, where the speed matters. Some context: Until Django 1.6 , when using South, I used the SOUTH_TESTS_MIGRATE setting: By default,

How to create GIN index in Django migration

孤街醉人 提交于 2019-11-28 02:56:32
问题 In Django, since version 1.11 we have a class for PostgreSQL GinIndex (https://docs.djangoproject.com/en/1.11/ref/contrib/postgres/indexes/). I'd like to create a migration that constructs such index on a VectorSearchField I added to one of my tables. So far, I've tried to simply add db_index=True to the VectorSearchField , but that fails, because it tries to create a B-Tree index (I think) and the VectorSearchField values are too long. I managed to create the index I want by running a

Creating Partial Indexes with Django 1.7

五迷三道 提交于 2019-11-27 22:56:43
The documentation for Django 1.7 mentions RunSQL classes can be used to create partial indexes on your tables. I have a table where I want the combination of title , blog & category to be unique. However if category is not provided, the combination of title & blog should still be unique. class Post(models.Model): title = models.CharField(max_length=200) blog = models.ForeignKey(Blog) category = models.ForeignKey(Category, null=True, blank=True) I can achieve this constraint with partial indexes (like the SQL shown below). Where do I add this code if I'm using Django 1.7 migrations? CREATE

Django migrate --fake and --fake-initial explained

旧街凉风 提交于 2019-11-27 22:47:55
I've been a user of Django for about 2 years now and there is a feature I have always been afraid of using : faking migrations . I've looked pretty much everywhere and the most information I can get is from the documentation where it states that: --fake Tells Django to mark the migrations as having been applied or unapplied, but without actually running the SQL to change your database schema. This is intended for advanced users to manipulate the current migration state directly if they’re manually applying changes; be warned that using --fake runs the risk of putting the migration state table

Django Sites Framework: Initial Data Migration Location

南笙酒味 提交于 2019-11-27 17:41:45
问题 Before Django 1.7, when using the Django Sites Framework one could/should define the initial data using Initial Fixtures. myproject/fixtures/initial_data.json [ { "pk": 1, "model": "sites.site", "fields": { "domain": "domain1", "name": "name1" } }, { "pk": 2, "model": "sites.site", "fields": { "domain": "domain2", "name": "name2" } }, { "pk": 3, "model": "sites.site", "fields": { "domain": "domain3", "name": "name3" } } ] Since it is a global project setting, I added a "fixtures" folder to

defining a custom post_migrate signal

落爺英雄遲暮 提交于 2019-11-27 16:23:24
问题 I'm doing some kind of refactoring for my project, where I'm relying on the django django.contrib.auth.models.Permission model. So far I define the permissions for each new user using a post_save signal, so when the user is created, I assign their permissions using user.user_permissions.add(the_permission) , this works perfectly. Now I want to use the django.contrib.auth.models.Group model to clasify the permissions a user should have. This is my code: from django.apps import AppConfig from

--fake-initial vs --fake in Django migration?

萝らか妹 提交于 2019-11-27 15:40:51
问题 What is the difference between --fake-initial and --fake in Django migrations? What are the dangers of using fake migrations? Anybody knows? Thank you very much to all. I am using django 1.10 回答1: Well the documentation is very clear about this --fake-initial Allows Django to skip an app’s initial migration if all database tables with the names of all models created by all CreateModel operations in that migration already exist. This option is intended for use when first running migrations

Can I use a database view as a model in Django?

陌路散爱 提交于 2019-11-27 11:52:39
i'd like to use a view i've created in my database as the source for my django-view. Is this possible, without using custom sql? ******13/02/09 UPDATE*********** Like many of the answers suggest, you can just make your own view in the database and then use it within the API by defining it in models.py. some warning though: manage.py syncdb will not work anymore the view need the same thing at the start of its name as all the other models(tables) e.g if your app is called "thing" then your view will need to be called thing_$viewname Since Django 1.1, you can use Options.managed for that. For