Niladry Kar
Niladry Kar

Reputation: 1203

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 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 == 'Business 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 == 'Business 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

Upvotes: 3

Views: 2763

Answers (2)

JPG
JPG

Reputation: 88499

Why self.id is None?

This is because Django assigns the id/pk values after the object creation, that is after calling the save() method of model.

So, call the save() method and then try to do your logic, as below

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

UPDATE
I for got one point that the instance should be again updated after calling the the save() method. So I had used the statement

self.__class__.objects.filter.(pk=self.pk).update(urlhash=urlhash)

Thanks to @sanip for the idea got from his answer

Upvotes: 7

Sanip
Sanip

Reputation: 1810

To get the id you need to try the answer provided by @JPG. But since the save() method is being called in initial step, the urlhash might not get updated as it is not saved in the instance. To update the urlhash, you can do something like:

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

Upvotes: 2

Related Questions