简体   繁体   中英

how change the size of the foreign key field in django admin?

I have a model with a CharField and a ForeignKey:

class FullName(models.Model):
     first_name = models.CharField(max_length=100)
     last_name  = models.ForeignKey(Name, on_delete=models.PROTECT)

in admin.py i am able to resize the "first_name" field to display whatever i want in localhost:8000/admin.

class NameAdmin(admin.ModelAdmin):
    
    formfield_overrides = {
        models.CharField: {'widget': TextInput(attrs={'size': '50'})},
    }

but i don't khow how to customize the size of the dropdown of the ForeignKey feild. i be grateful if someone can help me

# forms.py

from django.forms import ModelForm,TextInput,Select
from django.models import FullName

class FullNameForm(ModelForm):
    class Meta:
    model= FullName #Your Model Name
    fields = '__all__'

    

    widgets = {
        'first_name': TextInput(attrs={'size': '50'})),
        'last_name': Select(attrs={'size': '50'}))
    }

And in admin.py

from django.contrib import admin
from .models import FullName
from .forms import FullNameForm

class NameAdmin(admin.ModelAdmin):
    form = FullNameForm

admin.site.register(FullName, NameAdmin)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM