3

Fields Don't show in django admin While trying to add role through django admin it doesn't show the field

class Role(Core):
    role = models.CharField(max_length=25, unique=True, editable=False)

    def save(self, *args, **kwargs):
        self.role = self.role.lower()
        super(Role, self).save(*args, **kwargs)

    def __str__(self):
        return self.role.capitalize()

admin.site.register(Role)

class Core(models.Model):
    id = models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True

enter image description here

3
  • You should define Core before the Role. Commented May 29, 2021 at 18:08
  • furthermore the id is given by the database (it is the primary key) and the created_at and updated_at are items that are non-editable, so these will not show in the form either. Commented May 29, 2021 at 18:09
  • 1
    All your fields have editable=False, so as said above none would show up... Commented May 29, 2021 at 18:09

1 Answer 1

4

The id is given by the database (it is the primary key) and the created_at and updated_at are items that are non-editable, so these will not show in the form either.

This thus means that role would be the only field that can be used, but you specified this as editable=False [Django-doc], hence it will not show up to create/edit a Role object.

You should remove the editable=False part:

class Role(Core):
    #                             no editable = False ↓
    role = models.CharField(max_length=25, unique=True)

    def save(self, *args, **kwargs):
        self.role = self.role.lower()
        super(Role, self).save(*args, **kwargs)

    def __str__(self):
        return self.role.capitalize()
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.