简体   繁体   中英

How to change in Django Admin the default form (dropdown list) for a foreignKey model?

I want to change in my django administration page the form of a field. In my model I have the 'user' as foreign key to another model. When I create a new ' programare ' in the administration page the default form-field for user it's a dropdown with all entries of ' Cont ' model. It's dizzy to search the right one, so I want to change that dropdown with a plain-text input or an autocomplete form .

class Programare(models.Model):
   user = models.ForeignKey(to=Cont, on_delete=models.CASCADE, related_name='programari')
   ...

那个表单我想成为一个自动完成或至少像上面那个一样的纯文本。

I found the answer for my question. For django ^2.0 admin

class ProgramareAdmin(admin.ModelAdmin):
    model = Programare
    autocomplete_fields = ['user']

admin.site.register(Programare, ProgramareAdmin)

在此处输入图片说明

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