0

I want to create unique Personnel code for my users. I should consider this pattern in this code:

  1. Code length : 7 digit.
  2. first char from left side: get from USER_TYPE : user_type_code
  3. The next two char: current year example- 19 for 2019: year_code
  4. Four remaining char .create as count_code

I wrote whole generate but it don't work because of this error:

AttributeError: Manager isn't accessible via Profile instances

This is my profile model and generator:

class Profile(models.Model):
    USER_TYPE = (
    (1, 'student'),
    (2, 'teacher'),
    (3, 'co-worker'),)
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    Personnel_code = models.PositiveIntegerField(null=True, blank=True)
    created_date = models.DateField(editable=False)
    user_type = models.CharField(max_length=10, default=USER_TYPE[0][0],choices=USER_TYPE)

    def personnel_code_generator(self):
        user_type_code = self.user_type
        year_code = datetime.datetime.now().strftime("%y")
        last_user = self.objects.latest('created_date')
        year_user = self.objects.filter(created_at__year=datetime.datetime.now().year)
        if year_user == 0:
            count_code = '0001'
        else:
            count_code = str(int(last_user.Personnel_code) + 1)[3:]
        generated_id = int(str(user_type_code) + str(year_code) + count_code)
        return generated_id

    def save(self, *args, **kwargs):
        if not self.id or not self.created_date:
            self.personnel_code = self.personnel_code_generator()
            self.created_date = datetime.datetime.now()
        super(Profile, self).save(*args, **kwargs)
rahnama7m
  • 865
  • 10
  • 38

1 Answers1

1

Like the error says, you can not access the objects manager through a Profile object. You should access this through the Profile class, like:

def personnel_code_generator(self):
    user_type_code = self.user_type
    year_code = datetime.datetime.now().strftime("%y")
    year_users = Profile.objects.filter(created_at__year=datetime.datetime.now().year)
    if not year_user:
        count_code = '0001'
    else:
        last_user = Profile.objects.latest('created_date')
        count_code = str(last_user.Personnel_code + 1)[3:]
    return int(str(user_type_code) + str(year_code) + count_code)
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • Thank you. It worked. But know raise this error: `ValueError: invalid literal for int() with base 10: ''` @willem-van-onsem – rahnama7m Jun 10 '19 at 12:34
  • @Whale52Hz: that's because the logic of your `personnel_code_generator` is likely of. I think the `count_code` should be `str(..)[-3:]`, and furthermore probably it is not guaranteed that you pass something sensical to the `int(..)` here. – Willem Van Onsem Jun 10 '19 at 12:35
  • Thanks. I did accept your answer. Is it possible to answer this [question](https://stackoverflow.com/questions/56465295/how-can-display-the-label-of-non-editable-fields-in-django-template) too? @willem-van-onsem – rahnama7m Jun 10 '19 at 12:57