question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Model is imported with empty pk value

See original GitHub issue

Describe the bug I try to import a csv file via django admin with only one column which is also the primary key field for that model. The csv consist of 3 columns but only the first one is of any interest here. The import seems to work as I get a success message but if I look at the list view in django admin there is only one more entry with an empty key field.

To Reproduce Steps to reproduce the behavior: Prerequisite is the following model:

class Token(models.Model):
    key = models.CharField(db_index=True,unique=True,primary_key=True,    )
    state = models.PositiveSmallIntegerField(default=State.VALID, choices=State.choices)

then

class TokenResource(resources.ModelResource):
    class Meta:
        model = Token
        report_skipped = True
        import_id_fields = ("key",)
        fields = ("key",)

the django Admin entry:

class TokenAdmin(ImportMixin, admin.ModelAdmin):
    resource_class = TokenResource

and some csv: key;type;number ABCDEFG;Publication;101549 GFEDCBA;Publication;101549

then go to this model in django admin list view and click import. Choose file with sample data, csv and click import. On the preview you get table with import token

After clicking import you get the success message: token import success

Versions (please complete the following information):

  • Django Import Export: 2.7.0
  • Python 3.9
  • Django 3.2

Expected behavior A list with the imported Tokens should appear within the list view, with a filled key value

Details import_export.resources.ModelResource.init_instance --> return self._meta.model() returns an instance without a key although the key value has been passed with the row

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ratondeaucommented, Dec 22, 2021

Ok thanks, must have overlooked that. I cannot change this somewhere in the settings, right? So I will adapt the file.

0reactions
matthewhegartycommented, Dec 22, 2021

OK thanks - your issue is a simple one - you are using ‘;’ as the delimiter in your csv. If you change this to ‘,’ it will work:

2021-12-22_10-34

If you need to use semi-colon as the delimiter, there are instructions here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Snowflake - Primary key column is empty when importing from ...
I have a file that contains id column (which is the PK in the table I'm trying to load to) and a start...
Read more >
Why does Postgres generate an already used PK value?
The sequence is only evaluated when the id field is set as blank. But that is problem if there are already entries into...
Read more >
Models - Django documentation
Given a model instance, the display value for a field with choices can be accessed using the get_FOO_display() method. For example: from django.db...
Read more >
Common Issues with the SQL Server Import and Export Wizard
The SQL Server Import and Export Wizard is a useful tool for copying data from one data source (e.g. a SQL Server database...
Read more >
Create a Table with SQLModel - Use the Engine
metadata is still empty. But if you import the models before calling SQLModel.metadata.create_all() , it will work:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found