Home > Enterprise >  how to generate a custom id for specific type of users when that user signup via django model form
how to generate a custom id for specific type of users when that user signup via django model form

Time:11-20

models.py

 class CustomUser(AbstractUser):

username = None
email = models.EmailField(_('email address'), unique=True) 
mobileno = models.IntegerField(blank=True, null=True)    
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []

customer = models.BooleanField(default=False)
vendor = models.BooleanField(default=False)

id = models.AutoField(primary_key=True, editable=False)
userid= models.CharField(max_length=100, unique=True)

objects = UserManager()

def __str__(self):
    return self.email


def save(self, *args, **kwargs):
    super().save(*args, **kwargs)
    if self.vendor == True and self.is_superuser == False:                                   
        if not self.userid: 
            self.userid = "VEN"   str(self.id   (10 ** 5))  # generating the uid and allocating the value
            self.userid.save()
        else:
            pass                                        
    else:
        pass

forms.py

 class CustomUserCreationForm(UserCreationForm):
"""
A Custom form for creating new users.
"""

class Meta:
    model = CustomUser
    fields = ['email','first_name','last_name','mobileno']

def save(self, commit=True):
    user = super().save(commit=False)
    user.customer = True
    if commit:
        user.save()
    return user




 """ For Vendor users forms"""
 class VendorCreationForm(UserCreationForm):

class Meta:
    model = CustomUser
    fields = ['email','first_name','last_name','mobileno']


def save(self, commit=True):
    user = super().save(commit=False)
    user.vendor = True
    if commit:
        user.save()
    return user

views.py

  #Vendor Signup
  def VendorSignup(request):
     vendorform = VendorCreationForm()
     if request.method == 'POST':
         vendorform = VendorCreationForm(request.POST)        

         if vendorform.is_valid():
             new_user = vendorform.save()
             new_user.is_active = False
             new_user.save()
             return redirect('login')
     else:
         vendorform = VendorCreationForm()


     return render(request, 'vendor/signup.html', {'vendorform': vendorform})

I want to generate a customID (that starts with VEND0001)for the vendor users only when (they're signup with vendorform) and shouldn't generated customid for customers and superuser. I have an error in models. Can someone please, solve this for me.

I have facing issues (UNIQUE constraint failed: customer_customuser.userid).

CodePudding user response:

Maybe I'm wrong, but I think when you do:

def save(self, *args, **kwargs):
    super().save(*args, **kwargs)
    if self.vendor == True and self.is_superuser == False:                                   
        if not self.userid: 
            self.userid = "VEN"   str(self.id   (10 ** 5))  # generating the uid and allocating the value
            self.userid.save()
        else:
            pass                                        
    else:
        pass

In super().save there is no userid information which is obligatory. Maybe userid= models.CharField(max_length=100, unique=True, null=True) will work... But be aware that NULL != NULL.

Then change if not self.userid to if self.userid is None:

  • Related