Search code examples
djangodjango-usersdjango-import-export

Django import export extended User


I'm trying to use Django import-export to import data in my app. Even with the documentation, I can't make it work for my "Proprietaire" Model, which is extending Django's user model. Here my models:

class Proprietaire(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    adresse = models.CharField(max_length=500)
    telephone_regex = RegexValidator(
        regex="[0-9]{10}", message="Veuillez entrer un numéro de téléphone valide."
    )
    telephone = models.CharField(validators=[telephone_regex], max_length=10)
    date_inscription = models.DateField(auto_now_add=True)

The user import seems to be working fine but then I don't know how to import my Proprietaires and link them to my Users. After reading the documentation, I tried something like this in my admin file:

class ProprietaireResource(ModelResource):

    class Meta:
        model = Proprietaire
        fields = ('user__username','telephone','adresse')
        import_id_fields = ('user__username')


@admin.register(Proprietaire)
class ProprietaireAdmin(ImportExportModelAdmin):
    resource_class = ProprietaireResource

admin.site.unregister(User)

@admin.register(User)
class UserAdmin(ImportExportModelAdmin):
    pass

But whatever I try (without import_id_fields or without fields, or with adrresse as import_id_fields), I keep getting errors like:

Traceback (most recent call last):
File "C:\Users\perre\AppData\Local\Programs\Python\Python38\lib\site-packages\import_export\resources.py", line 500, in import_row
instance, new = self.get_or_init_instance(instance_loader, row)
File "C:\Users\perre\AppData\Local\Programs\Python\Python38\lib\site-packages\import_export\resources.py", line 277, in get_or_init_instance
instance = self.get_instance(instance_loader, row)
File "C:\Users\perre\AppData\Local\Programs\Python\Python38\lib\site-packages\import_export\resources.py", line 265, in get_instance
import_id_fields = [
File "C:\Users\perre\AppData\Local\Programs\Python\Python38\lib\site-packages\import_export\resources.py", line 266, in <listcomp>
self.fields[f] for f in self.get_import_id_fields()
KeyError: 'a'

I don't understand what's happening here.

For information, I'm trying to use the same file to import User and Proprietaire (I don't think that should be a problem); here's an example of a line I'm trying to import:

Headers : id,first_name,last_name,user__username,username,email,is_active,adresse,telephone Line : ,Clémentine,PerreautBis,clementine.perreautbis,clementine.perreautbis,example@test.fr,0,25 rue du Paradis 69003 Lyon,0203040506


Solution

  • I finally solved my problem. As it was important for me to keep it all in one file (the end user is supposed to fill the import files and I wanted to keep it as simple as possible for them) and I didn't think it was the problem, I looked up the widget part only to see if it would make a difference. And it did! After reading the following documentation: https://django-import-export.readthedocs.io/en/latest/api_widgets.html, my code looked like this:

    class ProprietaireResource(ModelResource):
        user = Field(column_name='user', attribute='user', widget=ForeignKeyWidget(User, 'username'))
        class Meta:
            model = Proprietaire
            fields = ('id','user','adresse','telephone')
    

    I renamed my column "user__username" as "user" and everything worked like expected :) Thanks for the advice about the ForeignKeyWidget.