I'm creating some instances of the Item
model shown below from a csv file. In the csv file only the name
and the filename
of the Item is specified. The image
field is set by looking if the image exists, and if it does, set the image file to image
:
I do:
item = Item()
item.name = csv_fields[0]
item.filename = csv_fields[1]
item.save()
...
f = open(settings.MEDIA_ROOT+"images/"+item.filename, 'r')
item.image = File(f)
item.save()
Doing so, the image is duplicated in settings.MEDIA_ROOT+"images/"
as <original filename>_1
. How can avoid that? i.e. how can I just set an existing file to an imagefield, without copying it?
class Item(models.Model):
name = models.CharField(max_length=50)
filename = models.CharField(max_length=100,)
image = models.ImageField(upload_to='images', blank=True, null=True)