首先为您希望使用的应用程序创建一个空迁移。就我而言,我有一个应用程序称为users
其中这种东西的生活,所以我跑:
manage.py makemigrations users --empty
创建我的迁移目录的新文件,我是能够用下面的内容更新:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
from rest_framework.authtoken.models import Token
from django.contrib.auth.models import User
def add_tokens(apps, schema_editor):
print "Adding auth tokens for the API..."
for user in User.objects.all():
Token.objects.get_or_create(user=user)
def remove_tokens(apps, schema_editor):
print "Deleting all auth tokens for the API..."
Token.objects.all().delete()
class Migration(migrations.Migration):
dependencies = [
('users', '0002_load_initial_data'),
]
operations = [
migrations.RunPython(add_tokens, reverse_code=remove_tokens),
]