Import-Export Import Django Import Duplicate value breaks error

I am working on a project with Django 1.10, Python 3.6 and PostgreSQL as a database in which I have a model called "Article" and I need to import data from CSV files. I imported my first CSV file successfully with the following fields: identifier, link and category Identification fields start from 1 to 1960, then in the next file I started the ID field from 1961, but it shows the following error:

Line number: 1961 - duplicate key value violates the unique restriction "article_article_pkey". DETAIL: Key (id) = (1961) already exists.

Even when I see an article model in the Django admin, it shows identifiers from 1-1960

Here are my models.py:

class Article(models.Model):
   id = models.AutoField(primary_key=True)
   link = models.URLField(max_length=255)
   category = models.CharField(max_length=255, choices=Categories)

Here admin.py

@admin.register(Article)
    class ArticleAdmin(ImportExportModelAdmin):
    resource_class = views.ArticleResource
    readonly_fields = ('id',)
+4
2

, : PostgreSQL, . , , , , , , .

, reset PostgreSQL, :

  • DB
  • id SELECT MAX(id) FROM your_table;
  • , ID : SELECT nextval('your_table_id_seq');
  • nextval - Max, . , MAX = 10 nextval = 11
  • reset id_seq :

    BEGIN;

    - .

    LOCK TABLE your_table IN EXCLUSIVE MODE;

    -

    SELECT setval('your_table_id_seq', COALESCE((SELECT MAX(id)+1 FROM your_table), 1), false);

    COMMIT;

0

, import_id_fields, id.

:

    import_id_fields = ('code',)
    skip_unchanged = True
    report_skipped = True

:

    import_id_fields = ('code',)
    skip_unchanged = True
    report_skipped = True
    exclude = ('id',)

, - Google.

Ref: https://github.com/django-import-export/django-import-export/issues/273

0

Source: https://habr.com/ru/post/1687771/


All Articles