Better ArrayField admin widget?

前端 未结 4 1918
抹茶落季
抹茶落季 2020-12-30 22:42

Is there any way to make ArrayField\'s admin widget allow adding and deleting objects? It seems that by default, it is instead displayed just a text field, and uses comma se

4条回答
  •  隐瞒了意图╮
    2020-12-30 22:49

    I take no credit for this (original source), but if you are using PostgreSQL as the database and are happy to use the Postgres-specific ArrayField implementation there is an even easier option: subclass ArrayField on the model and override the default admin widget. A basic implementation follows (tested in Django 1.9, 1.10, 1.11, 2.0, 2.1 & 2.2):

    models.py

    from django import forms
    from django.db import models
    from django.contrib.postgres.fields import ArrayField
    
    
    class ChoiceArrayField(ArrayField):
        """
        A field that allows us to store an array of choices.
        Uses Django's Postgres ArrayField
        and a MultipleChoiceField for its formfield.
        """
    
        def formfield(self, **kwargs):
            defaults = {
                'form_class': forms.MultipleChoiceField,
                'choices': self.base_field.choices,
            }
            defaults.update(kwargs)
            # Skip our parent's formfield implementation completely as we don't
            # care for it.
            # pylint:disable=bad-super-call
            return super(ArrayField, self).formfield(**defaults)
    
    
    FUNCTION_CHOICES = (
        ('0', 'Planning'),
        ('1', 'Operation'),
        ('2', 'Reporting'),
    )
    
    
    class FunctionModel(models.Model):
        name = models.CharField(max_length=128, unique=True)
        function = ChoiceArrayField(
            base_field=models.CharField(max_length=256, choices=FUNCTION_CHOICES),
            default=list)
    

提交回复
热议问题