2

I want to create a form widget like the permission selector in the admin page.

The model where i want to choose the records is a view from the database

class Ftp_Archivo(models.Model):
      id_lista = models.IntegerField(primary_key=True)
      id_proveedor = models.IntegerField()
      codigo_asignado = models.CharField(max_length=50, verbose_name='Codigo Asignado')
      nombre = models.CharField(max_length=150)
      archivo = models.CharField(max_length=500)

      class Meta:
            managed=False
            db_table='pc_archivos_no_migrados'

I want to save the records in this model:

class PYC_Archivo(models.Model):
      id_archivo = models.AutoField(primary_key=True)
      id_proveedor =  models.ForeignKey(Proveedor,db_column='id_proveedor',verbose_name='Proveedor')
      codigo_asignado = models.CharField(max_length=50, verbose_name='Codigo Asignado')
      nombre = models.CharField(max_length=150)
      archivo = models.CharField(max_length=500)
      fecha_migracion = models.DateField(auto_now = True,verbose_name='Fecha Migracion')

I don't know if is possible use a widget like the permission selector in admin page and which widget I need to use in my form.

joselegit
  • 533
  • 1
  • 14
  • 35

1 Answers1

3

I got it

I need to import the widget from admin.widgets and add the proper css and js files like this in the definition of the form

from django.contrib.admin.widgets import FilteredSelectMultiple

class fileform(forms.Form):
    archivos = forms.ModelMultipleChoiceField(queryset=Proveedor.objects.all(),required=True,
               widget=FilteredSelectMultiple("Proveedor",is_stacked=False))
 class Media:
        css = {
            'all':('/admin/css/widgets.css',),
        }
        # jsi18n is required by the widget
        js = ('/admin/jsi18n/',)

and in the template is necessary call the media files

{{form.media}}
Nitheesh MN
  • 608
  • 8
  • 18
joselegit
  • 533
  • 1
  • 14
  • 35