django-migrations

Django 1.7 data migration for custom user model

故事扮演 提交于 2019-11-30 20:58:54
问题 I have created a custom user model for my project. I have written a data migration file to copy contents of auth.User to my new custom user table. It doesn't seem to work properly. It is throwing below error, from django.db import migrations, transaction @transaction.atomic def copy_old_users(apps, schema_editor): User = apps.get_model("auth", "User") CustomUser = apps.get_model("custom_auth", "User") fields = ['id', 'username', 'email', 'first_name', 'last_name', 'is_staff', 'is_active',

Convert data on AlterField django migration

有些话、适合烂在心里 提交于 2019-11-30 15:08:33
问题 I have a production database and need to keep safe the data. I want to change a Field in model and convert all data inside that database with this change. Old field class MyModel(models.Model): field_name = models.TimeField() Changed field class MyModel(models.Model): field_name = models.PositiveIntegerField() Basically I want to convert the TimeField value (that has a Time object) in minutes. Example: I have in an object time(hour=2, minute=0, second=0) and I want to convert that field value

Convert data on AlterField django migration

纵饮孤独 提交于 2019-11-30 14:30:25
I have a production database and need to keep safe the data. I want to change a Field in model and convert all data inside that database with this change. Old field class MyModel(models.Model): field_name = models.TimeField() Changed field class MyModel(models.Model): field_name = models.PositiveIntegerField() Basically I want to convert the TimeField value (that has a Time object) in minutes. Example: I have in an object time(hour=2, minute=0, second=0) and I want to convert that field value in all database table to 120 when I apply the migrate. Safest way that I always do is: create another

How do I execute raw SQL in a django migration

半城伤御伤魂 提交于 2019-11-30 13:42:59
问题 I am aware of the cursor object in Django. Is there any other preferred way to execute raw SQL in migrations? I want to introduce postgresql partitioning for one of my models tables. The partition logic is a bunch of functions and triggers that have to be added to the database on setup which I'd like to automate. 回答1: One way: The best way I found to do this is using RunSQL: Migrations contains the RunSQL class. To do this: ./manage.py makemigrations --empty myApp edit the created migrations

How to add through option to existing ManyToManyField with migrations and data in django

戏子无情 提交于 2019-11-30 11:04:58
I can't find reference to particular issue in docs or online. I have an existing many to many relation. class Books(models.Model): name = models.CharField(max_length=100) class Authors(models.Model): name = models.CharField(max_length=100) books = models.ManyToManyField(Books) This has migrations and data. Now I need to use through option in order to add one extra field in table holding many to many relation. class Authorship(models.Model): book = models.ForeignKey(Books) author = models.ForeignKey(Authors) ordering = models.PositiveIntegerField(default=1) class Authors(models.Model): name =

How do I execute raw SQL in a django migration

不羁岁月 提交于 2019-11-30 08:19:31
I am aware of the cursor object in Django. Is there any other preferred way to execute raw SQL in migrations? I want to introduce postgresql partitioning for one of my models tables. The partition logic is a bunch of functions and triggers that have to be added to the database on setup which I'd like to automate. One way: The best way I found to do this is using RunSQL: Migrations contains the RunSQL class. To do this: ./manage.py makemigrations --empty myApp edit the created migrations file to include: operations = [ migrations.RunSQL('RAW SQL CODE') ] As Nathaniel Knight mentioned, RunSQL

How to squash recent Django migrations?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 05:52:16
In Django's migrations code, there's a squashmigrations command which: "Squashes the migrations for app_label up to and including migration_name down into fewer migrations, if possible." So, if you want to squash, say, the first 5 migrations, this will help. What's the best way to squash starting with a particular migration_name ? In a project I'm currently working on, we've added 5-10 new migration files as we've added new features. We'll deploy the whole project at once and it looks like running these individually will take too long. I'd like to squash all the migrations for this project

Deleting unused Models, stale content types prompt

会有一股神秘感。 提交于 2019-11-30 03:13:49
问题 I am removing an unnecessary table and model from our Django website. I have removed all foriegn key references before the migrations.DeleteModel(...) is called, but I still am receiving the following prompt when I run the migration: The following content types are stale and need to be deleted: myapp | MyDeletedModel Any objects related to these content types by a foreign key will also be deleted. Are you sure you want to delete these content types? If you're unsure, answer 'no'. Type 'yes'

Django backup strategy with dumpdata and migrations

可紊 提交于 2019-11-29 21:08:23
问题 As in this question, I set up a dumpdata -based backup system for my database. The setup is akin to running a cron script that calls dumpdata and moves the backup to a remote server, with the aim of simply using loaddata to recover the database. However, I'm not sure this plays well with migrations. loaddata now has an ignorenonexistent switch to deal with deleted models/fields, but it is not able to resolve cases where columns were added with one-off defaults or apply RunPython code. The way

Django 1.7 Migrations

淺唱寂寞╮ 提交于 2019-11-29 16:21:03
问题 I am using django 1.7 and I just added a custom user model. When I run either python3 manage.py makemigrations or python3 manage.py migrate I get the error: TypeError: __init__() got an unexpected keyword argument 'preserve_default' . This issue came along after adding the new custom user model. The complete traceback is: Traceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/usr/local/lib/python3.4/dist-packages/django/core