Update thousands of records at once is challenging as default.
One issue is the performance on the database side. the best I could come up is
using update
and overwriting some methods.
assume you already have your images in some directory.
from django.db.models import fields
from django.db.models import F
from django.db.models.expressions import Value , CombinedExpression
from django.db.models import QuerySet
class TextValue(Value):
def as_sql(self, compiler, connection):
connection.ops.check_expression_support(self)
return '%s', [self.value]
class Expr(F):
ADD = '||' # standard concat row value + value in PostgreSQL
#overwrite method to support text
def _combine(self, other, connector, reversed):
if not hasattr(other, 'resolve_expression'):
other = TextValue(other, output_field=fields.CharField())
return CombinedExpression(self, connector, other)
class Entry(models.Model):
name = models.CharField(unique=True, max_length=200, db_index=True)
image = models.ImageField(upload_to= 'media/' , null=True, blank=True , default='default.png')
now having this piece code you can bulk update
entries= Entry.objects.all()
entries.update(**{'image': Expr('name') + '.png'})
the best part of doing it like is the performance. this is the only query that is executed
{'sql': 'UPDATE "entry" SET "image" = ("entry"."name" || \'.png\')', 'time': '0.024'}]
update
doing the same thing as admin and saving one instance of model per entry.
from django.contrib import admin
from django import forms
class EntryForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(EntryForm, self).__init__(*args, **kwargs)
self.fields['image'].widget.attrs.update(
{'multiple': True, 'accept': 'image/jpg,image/png,image/gif', })
class EntryAdmin(admin.ModelAdmin):
form = EntryForm
def save_model(self, request, obj, form, change):
files = request.FILES.getlist('image')
# if image exist
if files:
for image_field in files:
try:
instance = Entry.objects.get(name=image_field.name[:-4])
instance.image = image_field
instance.save()
except Entry.DoesNotExist:
pass
else:
return super().save_model(request, obj, form, change)
admin.site.register(Entry , EntryAdmin)
you can mix these two part to get batter performance