#django #django-forms #autocomplete #formset #inline-formset
Вопрос:
Я использую django-extra-views и хочу реализовать функцию автозаполнения для некоторых полей an InlineFormSetFactory
. Я пробовал с помощью django-ajax-selects, но это не сработало… Есть ли простой способ сделать это?
Мой 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'
Мой 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')