Django: The value of the id field is coming None

Django: The value of the id field is coming None

I want to create a unique id for all my groups in my django project which will help us to easily detect the user action.

I want to create a unique id for all my groups in my django project which will help us to easily detect the user action.

I have tried the following:

Models:

class Group1(models.Model):
    urlhash = models.CharField(max_length=6, null=True, blank=True, unique=True)
    user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,null=True,blank=True)
    group_name = models.CharField(max_length=32)

This is the functionality I have given:

def save(self):
    if not self.urlhash:
       if self.user.profile.user_type == 'Bussiness User':
           self.urlhash = 'B' + str(self.user.id) + ('00') + str(self.id)
       else:
           self.urlhash = 'P' + str(self.user.id) + ('00') + str(self.id)
super(Group1, self).save()

But str(self.id) is coming None.

Can anyone plz explain me why this error is happening?

Thank you

According to the solution given by @JPG and @sandip I finally got the solution

def save(self):
   super(Group1, self).save()
   if not self.urlhash:
       if self.user.profile.user_type == 'Bussiness User':
           self.urlhash = 'B' + str(self.user.id) + ('00') + str(self.id)
           Group1.objects.filter(pk=self.pk).update(urlhash=self.urlhash)
       else:
           self.urlhash = 'P' + str(self.user.id) + ('00') + str(self.id)
           Group1.objects.filter(pk=self.pk).update(urlhash=self.urlhash)

This works for me...Thank you everyone

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Python Django Tutorial | Django Course

🔥Intellipaat Django course: https://intellipaat.com/python-django-training/ 👉This Python Django tutorial will help you learn what is django web development &...

How to Integrate Django to any Javascript Front End // Django to React // Django to Angular

Learn the basic technique to Integrate Django to any Javascript Front End // Django to React // Django to Angular

Django

Well i used django ... i want to know after completing all the coding stuff, can i make all the codes online ?