[Fixed]-How to validate array of choices in Django ModelForm against preset list of choices

1👍

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

Leave a comment