Skip to content

verbose name neglect by formfield_for_foreignkey . #45

@ritiksoni00

Description

@ritiksoni00

if we had given a verbose name for the model field and if we try to customize our FK representation in the admin panel then we will use formfield_for_foreignkey as shown in the block below

def formfield_for_foreignkey(self, db_field, request, **kwargs):
    if db_field.name == 'category':
        return CategoryChoiceField(queryset=Category.objects.all())
    return super().formfield_for_foreignkey(db_field, request, **kwargs)

and here it will neglect the given verbose name (tbh IDK why its happening maybe DB layer is overridden by the form layer!)
so we have to provide the label argument again

def formfield_for_foreignkey(self, db_field, request, **kwargs):
    if db_field.name == 'category':
        return CategoryChoiceField(queryset=Category.objects.all(), label='_XYZ_')
    return super().formfield_for_foreignkey(db_field, request, **kwargs)

link

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions