我在更新我运行的网站时遇到了问题。从本质上讲,网站上的用户不能再使用了,所以人们必须创建新的帐户。但是,用户与数据库中的其他表具有ManyToMany关系,我希望能够将这些关系从旧用户移动到新用户。
有没有在Django中或直接在数据库中完成此操作的好方法?我正在使用PostgreSQL。
发布于 2021-01-07 19:12:10
Django为此提供了一个管理命令框架。https://docs.djangoproject.com/en/3.1/howto/custom-management-commands/
它应该位于PROJECT/APPNAME/management/commands/中
from django.core.management import BaseCommand
from django.contrib.auth import get_user_model
User = get_user_model()
#The class must be named Command, and subclass BaseCommand
class Command(BaseCommand):
# Show this when the user types help
help = "fix users relations"
# A command must define handle()
def handle(self, *args, **options):
self.stdout.write("fixing users begin")
for user in User.objects.all():
...user fix code...
self.stdout.write("fix users complete")https://stackoverflow.com/questions/65610952
复制相似问题