简体   繁体   English

在创建模型记录时创建django用户

[英]create a django user while creating a records for model

models.py as below, 如下的models.py

from django.db import models
from django.contrib.auth.models import User

class members(models.Model):

    auto_id = models.AutoField(primary_key=True)
    member_name = models.OneToOneField(User)
    owner = models.ForeignKey('auth.User', related_name='webapi', on_delete=models.CASCADE)
    father_name = models.CharField(max_length=25, blank=False, default='')
    wife_name = models.CharField(max_length=25, blank=False, default='')
    number_of_child = models.IntegerField(blank=True)
    address_line_1 = models.CharField(max_length=40, blank=False, default='')
    address_line_2 = models.CharField(max_length=40, blank=True, default='')
    city = models.CharField(max_length=25, blank=False, default='')

    class Meta:
        ordering = ('member_name',)

Now The above has been linked with django auth Users table and in steriliser it shows the existing Django users and I have to choose one during the submit. 现在,以上已与django auth Users表链接,并且在steriliser中它显示了现有的Django用户,我必须在提交期间选择一个。

But my requirement is as a admin user I login and then provide the member_name manually which should automatically create a django user also 但是我的要求是作为管理员用户登录,然后手动提供member_name,这也将自动创建一个django用户

django-annoying has a feature for this, specifically AutoOneToOneField . django-annoying具有此功能,特别是AutoOneToOneField

Sample from their github: 来自他们的github的示例:

from annoying.fields import AutoOneToOneField

class MyProfile(models.Model):
    user = AutoOneToOneField(User, primary_key=True)

This should automatically create a User. 这将自动创建一个用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM