Home > front end >  python, django, display the correct field name by FK, linked tables
python, django, display the correct field name by FK, linked tables

Time:11-18

models.py file

#create class roles, id, portal_name, role_name
class Roles(models.Model):
    portal_name = models.CharField(_('Portal name'), max_length=100)
    role_name = models.CharField(_('Role_name'), max_length=50)


#create permission for roles
class RolePermission(models.Model):
    module_name = models.CharField(_('Module name'), max_length=100)
    module_delete = models.BooleanField(default=False, help_text="Delete module. Default false, can't delete")
    module_edit = models.BooleanField(default=False, help_text="Edit module. Default false, can't edit")
    module_create = models.BooleanField(default=False, help_text="Create module. Default false, can't create")
    module_submit = models.BooleanField(default=False, help_text="Submit module, Default false.")
    module_role_id = models.ForeignKey('Roles', on_delete=models.CASCADE)

enter image description here

enter image description here

my admin.py file

@admin.register(RolePermission)
class RolePermissionAdmin(admin.ModelAdmin):
    list_display = ('module_name', 'module_role_id_id', 'module_delete', 'module_edit', 'module_create', 'module_submit')

as in the column display information on role_name from table roles, used Foreign Key

CodePudding user response:

You can define a method, and use the name of that method in the list_display. For this specific case we thus the role_name of the related .module_role_id:

@admin.register(RolePermission)
class RolePermissionAdmin(admin.ModelAdmin):
    list_display = ('module_name', 'role_name', 'module_delete', 'module_edit', 'module_create', 'module_submit')

    @admin.display(description='role name')
    def role_name(self, obj):
        return obj.module_role_id.role_name

Note: Normally one does not add a suffix _id to a ForeignKey field, since Django will automatically add a "twin" field with an _id suffix. Therefore it should be module_role, instead of module_role_id.

CodePudding user response:

you can make new property and use it in list_display, it should be like this :

class RolePermission(models.Model):
    module_name = models.CharField(_('Module name'), max_length=100)
    module_delete = models.BooleanField(default=False, help_text="Delete module. Default false, can't delete")
    module_edit = models.BooleanField(default=False, help_text="Edit module. Default false, can't edit")
    module_create = models.BooleanField(default=False, help_text="Create module. Default false, can't create")
    module_submit = models.BooleanField(default=False, help_text="Submit module, Default false.")
    module_role_id = models.ForeignKey('Roles', on_delete=models.CASCADE)
    
    def  module_role(self):
        return self.module_role_id.role_name

and your list_display should be like this:

    list_display = ('module_name', 'module_role', 'module_delete', 'module_edit', 'module_create', 'module_submit')
  • Related