0

I installed the module Django Import / Export link the installation went smoothly. Now when I want to import a file with the extension .xls it shows me the following error:

AttributeError at /admin/xxxx/xxxx/process_import/ 'float' Has No object attribute 'split'

Exception Location: C:\Python34\lib\site-packages\import_export\widgets.py in clean, line 321

When I edit the file here widgets.py source code

    def clean(self, value):
        if not value:
            return self.model.objects.none()
        if isinstance(value, float):
            ids = [int(value)]
        else:
            ids = value.split(self.separator)
        ids = filter(None, value.split(self.separator))
        return self.model.objects.filter(**{
            '%s__in' % self.field: ids
        })

Here are the lines 321 ids = filter(None, value.split(self.separator))

Django models

class Vehicule(models.Model):
    matricule = models.CharField(max_length=200)
    modele = models.CharField(max_length=200)
    annee = models.IntegerField()

    def __str__(self):
        return self.matricule

class Ligne(models.Model):
    nom = models.CharField(max_length=200)
    vehicule = models.ManyToManyField(Vehicule, through='Affecter_Vehicule_Ligne')

    def __str__(self):
        return str(self.nom)

class Affecter_Vehicule_Ligne(models.Model):
    vehicule = models.ForeignKey(Vehicule, on_delete=models.CASCADE)
    ligne = models.ForeignKey(Ligne, on_delete=models.CASCADE)
    actif = models.BooleanField(default=False)
    dateAffectation = models.DateTimeField(null=True)

    def __str__(self):
        return str(self.dateAffectation)


class Arret(models.Model):
   nom = models.CharField(max_length=200, null=True)
   latitude = models.CharField(max_length=200, null=True)
   longitude = models.CharField(max_length=200, null=True)
   lignes = models.ManyToManyField(Ligne, through='Ligne_Arret')

   def __str__(self):
       return str(self.nom)

class Ligne_Arret(models.Model):
    sens = models.CharField(max_length=200)
    section = models.BooleanField(default=False)
    ligne = models.ForeignKey(Ligne, on_delete=models.CASCADE)
    arret = models.ForeignKey(Arret, on_delete=models.CASCADE)

    def __str__(self):
        return str(self.arret)

Django admin

class VehiculeAdmin(admin.ModelAdmin):
    list_display = ('matricule', 'modele', 'annee')
    search_fields = ('matricule', 'modele')

class Affecter_Vehicule_LigneAdmin(admin.ModelAdmin):
    list_display = ('vehicule', 'dateAffectation', 'ligne')

class ArretAdmin(ImportExportModelAdmin):
    pass

class Ligne_ArretAdmin(admin.ModelAdmin):
    list_display = ('ligne', 'arret', 'section', 'sens')

admin.site.register(Vehicule, VehiculeAdmin)
admin.site.register(Ligne)
admin.site.register(Affecter_Vehicule_Ligne, Affecter_Vehicule_LigneAdmin)
admin.site.register(Arret, ArretAdmin)
admin.site.register(Ligne_Arret, Ligne_ArretAdmin)

Help me please to solve this problem ???

Mane alioune
  • 33
  • 1
  • 12

1 Answers1

0

You are trying to split a float value in this line ids = filter(None, value.split(self.separator))

I think you can just remove this line. As you handle the None case and split before.

ilse2005
  • 11,189
  • 5
  • 51
  • 75
  • Thank you for your reply now when i remove this line another error appears Cannot set values on a ManyToManyField which specifies an intermediary model. Use transports.Ligne_Arret's Manager instead. – Mane alioune Apr 05 '16 at 12:30
  • The code about that error is missing in your question. The error occurs because you use the `through` argument in your ManyToMany relation. Then you can't use `add` to create relations. See the docs about that: https://docs.djangoproject.com/es/1.9/topics/db/models/#extra-fields-on-many-to-many-relationships – ilse2005 Apr 05 '16 at 12:38
  • Thanks you I read this documentation but when i import a file .xls this error appears AttributeError at /admin/xxxx/xxxx/process_import/ Cannot set values on a ManyToManyField which specifies an intermediary model. Use transports.Ligne_Arret's Manager instead. Exception Location: C:\Python34\lib\site-packages\django\db\models\fields\related_descriptors.py in set, line 882.... – Mane alioune Apr 05 '16 at 13:01
  • what seems strange in all this code worked perfectly now when i edit the file related_descriptors.py def set(self, objs, **kwargs): if not rel.through._meta.auto_created: opts = self.through._meta raise AttributeError( "Cannot set values on a ManyToManyField which specifies an " "intermediary model. Use %s.%s's Manager instead." % (opts.app_label, opts.object_name) ) – Mane alioune Apr 05 '16 at 13:04