Update
I have filed a feature request. The idea is to pass
on the IntegrittyError
produced by the database when unique
or unique_together
reject a record that already exists in the database.
I have the following model:
class Compositions(models.Model):
composer_key = models.ForeignKey(
Composer,
)
composition = models.CharField(
max_length=383,
)
class Meta(object):
unique_together = (('composer_key', 'composition'), )
Using django-import-export in the admin interface, without providing an id
for each entry in the csv file, ... if one pair of the csv file already exists, the procedure will be interrupted with an integrity error
duplicate key value violates unique constraint "data_compositions_composer_key_id_12f91ce7dbac16bf_uniq"
DETAIL: Key (composer_key_id, composition)=(2, Star Wars) already exists.
The CSV file is the following:
id composer_key composition
1 Hot Stuff
2 Star Wars
The idea was to use skip_row
and implement it in the admin.
admin.py:
class CompositionsResource(resources.ModelResource):
class Meta:
model = Compositions
skip_unchanged = True
report_skipped = True
class CompositionsAdmin(ImportExportModelAdmin):
resource_class = CompositionsResource
admin.site.register(Compositions, CompositionsAdmin)
This will not cure the problem, however, because skip_row
expects an id
in the csv file in order to check if each row is the same with the very specific database entry.
Considering that this control can be performed by the database when using unique
(_together
) would not it be effective to catch this error and then return skip_row = True
or alternatively pass
on this error?
pk
attribute is what differs? – Dandruffunique_together
will make any difference, since the implemetation is correct. I will give it a try after work. – Patonid
column empty? If so, when checking for a duplicate, a module might fool himself, because the new object's pk is different than of that in database. So it tries to add another record, and yes, fails because ofunique_together
constraint. I'd recommend you not to remove the constraint, but first try to assign a permanentid
s to your csv records. Either way, let me know how it went ) – Dandruffauto_now
field, only the date field is updated. Without it, as in my example, it works as expected. In my case, it is unrealistic to provide theid
: How can I make import_exportpass
on the integrity error raised? – Patonexclude = ('id',)
? – NobleIntegrittyError
. Excluding theid
will not help in this particular case because the database raises this error if a duplicate entry exists in the column, even with another id. – Paton