在Django中自动为新的教师创建新用户

编程入门 行业动态 更新时间:2024-10-09 16:30:02
本文介绍了在Django中自动为新的教师创建新用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用 tenant-schema 包开发多租户Web应用程序。一切正常,这里缺少的是自动为新租户创建新用户的一件事。我知道我们可以使用此命令创建新的超级用户

I'm trying to develop multi tenants web applications using tenant-schema packages. Every things is working fine, Here one thing is missing that is automatically create new user for new tenants. I know we can create new super user using this command

python manage.py tenant_command createsuperuser --schema=schema_name

但是我想根据用户提供的信息自动创建新用户

But i want to automatically create new user on basis of information provided by the user

这里我正在使用api创建租户,

Here I'm creating tenants using api,

api_view.py def post(self, request): serializer = ClientSerializer(data=request.data) if serializer.is_valid(): try: serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) except Exception as e: return Response({'info': 'Unable to create tenant'}) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

租户模型

class Client(TenantMixin): name = models.CharField(max_length=100) paid_until = models.DateField() on_trial = models.BooleanField() created_on = models.DateField(auto_now_add=True) auto_create_schema = True

serializers.py

serializers.py

from rest_framework import serializers from .models import Client class ClientSerializer(serializers.ModelSerializer): class Meta: model = Client fields = ['id', 'domain_url', 'schema_name', 'name', 'paid_until', 'on_trial', 'created_on']

在这里,我想自动创建新的超级用户。我不知道该怎么做,任何建议都将不胜感激。

Here i want to create new superuser automatically. I didn't know how to do, any suggestion would be appreciated.

推荐答案

您可以使用django 发布保存信号。

You could use django post save signals.

例如在models.py中:

For example in models.py:

from django.dispatch import receiver from tenant_schemas.utils import tenant_context from django.contrib.auth.models import User @receiver(post_save, sender=Client): def create_superuser(instance, **kwargs): if 'created' in kwargs: # tests if this client was created tenant=instance with tenant_context(tenant): # Create the superuser by using the new client tenant schema User.objects.create_user( # insert your user data here )

更多推荐

在Django中自动为新的教师创建新用户

本文发布于:2023-11-28 18:00:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643401.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:新用户   教师   Django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!