AndrewIngram / django-extra-views

Django's class-based generic views are awesome, let's have more of them.
MIT License
1.38k stars 172 forks source link

Autocomplete using InlineFormSetFactory #239

Closed eudaldt closed 2 years ago

eudaldt commented 2 years ago

I want to implement an autocomplete functionality to some fields of an InlineFormSetFactory. I tried with https://github.com/crucialfelix/django-ajax-selects but it didn't work... Is there an easy way to do it?

My models.py:


class Index(models.Model):
    id_index = models.AutoField(primary_key=True)
    index_name = models.CharField(max_length=30)
    index_id_index_type = models.ForeignKey(IndexType, on_delete=models.CASCADE, db_column='id_index_type', verbose_name="Tipus d'índex")
    index_id_index_provider = models.ForeignKey(IndexProvider, on_delete=models.CASCADE, db_column='id_index_provider', verbose_name="Proveïdor d'índex")
    miseq = models.CharField(max_length=9)
    nextseq = models.CharField(max_length=9)

    class Meta:
        db_table = 'index'
        unique_together = (("index_name", "index_id_index_type", "index_id_index_provider", "miseq", "nextseq"),)

    def __str__(self):
        return self.index_name

class GeneCandList(models.Model):
    id_gene_cand_list = models.AutoField(primary_key=True)
    name = models.CharField(unique=True, max_length=150, verbose_name="Llista de gens candidats")

    class Meta:
        db_table = 'gene_cand_list'

    def __str__(self):
        return self.name

class Pool(models.Model):
    id_pool = models.AutoField(primary_key=True)
    name = models.CharField(unique=True, max_length=50, verbose_name="Pool")
    hibridation = models.BooleanField(verbose_name="Hibridació OK?")
    samples = models.ManyToManyField('Sample', through='SamplePoolIndexCand', blank=True, verbose_name="Mostres"

    class Meta:
        ordering = ['-id_pool']
        db_table = 'pool'

    def __str__(self):
        return self.name

class Sample(models.Model):
    id_sample = models.AutoField(primary_key=True)
    name = models.CharField(unique=True, max_length=20)
    sample_id_sex = models.ForeignKey(Sex, on_delete=models.CASCADE, db_column='id_sex', verbose_name='Sexe')
    indexes = models.ManyToManyField(Index, through='SamplePoolIndexCand', through_fields=('sample_id', 'index_id'), blank=True, verbose_name="Índexs")
    pools = models.ManyToManyField(Pool, through='SamplePoolIndexCand', through_fields=('sample_id', 'pool_id'), blank=True, verbose_name="Pools")
    gene_cand_lists = models.ManyToManyField(GeneCandList, through='SamplePoolIndexCand', through_fields=('sample_id', 'gene_cand_list_id'), blank=True, verbose_name="Llista de gens candidats")

    class Meta:
        db_table = 'sample'

    def __str__(self):
        return self.name

class SamplePoolIndexCand(models.Model):
    sample_id = models.ForeignKey(Sample, null=True, blank=True, on_delete=models.CASCADE, db_column='id_sample', verbose_name='Mostra')
    pool_id = models.ForeignKey(Pool, null=True, blank=True, on_delete=models.CASCADE, db_column='id_pool', verbose_name='Pool')
    index_id = models.ForeignKey(Index, null=True, blank=True, on_delete=models.CASCADE, db_column='id_index', verbose_name='Índex')
    gene_cand_list_id = models.ForeignKey(GeneCandList, null=True, blank=True, on_delete=models.CASCADE, db_column='id_gene_cand_list', verbose_name='Llista de gens candidats')

    class Meta:
        unique_together = (("sample_id", "pool_id", "index_id", "gene_cand_list_id"),)
        db_table = 'sample_pool_index_cand'

My views.py:


class PoolIndexCandInLine(InlineFormSetFactory):
    model = SamplePoolIndexCand
    pool_id = AutoCompleteSelectField('pool_tag')    
    index_id = AutoCompleteSelectField('index_tag')
    gene_cand_list_id = AutoCompleteSelectField('list_tag')
    fields = ["pool_id", "index_id", "gene_cand_list_id",]

class SampleForm(CreateWithInlinesView):
    model = Sample
    inlines = [PoolIndexCandInLine]
    fields = ["name", "sample_id_sex",]
    template_name = 'sample/formulari_mostra.html'

    success_url = reverse_lazy('Sample form')   
sdolemelipone commented 2 years ago

I don't have any experience of using django-ajax-selects but I find https://github.com/codingjoe/django-select2 works pretty well for my inlines.

eudaldt commented 2 years ago

I'll try it, thanks!

eudaldt commented 2 years ago

I don't have any experience of using django-ajax-selects but I find https://github.com/codingjoe/django-select2 works pretty well for my inlines.

I'd appreciate an example, I installed it but something doesn't work.

eudaldt commented 2 years ago

I found the problem.