简体   繁体   中英

How to validate array of choices in Django ModelForm against preset list of choices

I have the following model form in my application which features a multiple choice field (think lots of checkboxes) populated from a static array of choices. I want users to be able to select multiple items and the result will be stored in the database as a string separated by commas but the Django validation keeps showing (for example):

>>> bsf = BiomSearchForm({"otu_text": ["Test"], "criteria":"soil"})
>>> bsf.errors
{'otu_file': ['This field is required.']}
>>> bsf = BiomSearchForm({"otu_text": ["Test"], "criteria":["soil", "geothermal"]})
>>> bsf.errors
{'otu_file': ['This field is required.'], 'criteria': ["Select a valid choice. ['soil', 'geothermal'] is not one of the available choices."]}

These choices are stored in the database for record purposes, they are not tied to any other tables. Is there a way to iterate through the submitted multiple choice array and check if the constituent strings are of the available choices? I found out that if we pass a single string instead of an array, it will validate correctly.

Here are my model and its form:

class BiomSearchJob(models.Model):
    ECOSYSTEM_CHOICES = (
        ("all", "All Ecosystem"),
        ("animal", "Animal/Human"),
        ("anthropogenic", "Anthropogenic"),
        ("freshwater", "Freshwater"),
        ("marine", "Marine"),
        ("soil", "Soil"),
        ("plant", "Plant"),
        ("geothermal", "Geothermal"),
        ("biofilm", "Biofilm"),
    )

    user = models.ForeignKey(User, on_delete=models.CASCADE)
    completed = models.BooleanField(default=False)
    criteria = models.CharField(
        default="all", choices=ECOSYSTEM_CHOICES, max_length=200,
    )
    otu_text = models.TextField(default=None)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def set_completed(self, completed):
        self.completed = completed

class BiomSearchForm(forms.ModelForm):
    class Meta:
        model = BiomSearchJob
        fields = {
            "otu_text": forms.CharField,
            "otu_file": forms.FileField,
            "criteria": forms.MultipleChoiceField(
                required=True,
            ),
        }
        labels = {
            "otu_text": "Paste your BIOM table",
            "criteria": "Select the ecosystem",
        }
        widgets = {
            "otu_text": forms.Textarea(attrs={'cols': 30, 'rows': 12}),
            "criteria": forms.CheckboxSelectMultiple,
        }

    otu_file = forms.FileField(
        label="or upload your BIOM file",
    )

This is a use case for ManyToMany relationship between BiomSearchJob and EcosystemChoices. This will implement an intermediate table for you under the covers.

EDIT: Adding an example implementation below:

class BiomSearchJob(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    completed = models.BooleanField(default=False)
    criteria = models.ManyToManyField('EcosystemChoices', related_name='%(app_label)s_%(class)s_prs', blank=True)
    otu_text = models.TextField(default=None)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def set_completed(self, completed):
        self.completed = completed


class EcosystemChoices(models.Model):
    ecosystem = models.CharField(verbose_name=u'Ecosystem Type', max_length=60, help_text='Select all that apply')

Pre-fill the table EcosystemChoices with your defined choices. This may help: Django ManyToMany Field

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