Django admin choice field

前端 未结 7 1664
日久生厌
日久生厌 2020-12-18 22:48

I have a model that has a CharField and in the admin I want to add choices to the widget. The reason for this is I\'m using a proxy model and there are a bunch of models tha

7条回答
  •  佛祖请我去吃肉
    2020-12-18 23:07

    You don't need a custom form.

    This is the minimum you need:

    # models.py
    from __future__ import unicode_literals
    
    from django.db import models
    
    class Photo(models.Model):
        CHOICES = (
            ('hero', 'Hero'),
            ('story', 'Our Story'),
        )
    
        name = models.CharField(max_length=250, null=False, choices=CHOICES)
    
    # admin.py
    from django.contrib import admin
    from .models import Photo
    
    
    class PhotoAdmin(admin.ModelAdmin):
        list_display = ('name',)
    
    
    admin.site.register(Photo, PhotoAdmin)
    

提交回复
热议问题