简体   繁体   中英

create a django user while creating a records for model

models.py as below,

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.

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

django-annoying has a feature for this, specifically AutoOneToOneField .

Sample from their github:

from annoying.fields import AutoOneToOneField

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

This should automatically create a User.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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