Use a materialized Extension.latest_version field instead of a dynamic property #152
@ -17,6 +17,7 @@ def valid_file_statuses(self):
|
||||
|
||||
def populate_latest_version(apps, schema_editor):
|
||||
Extension = apps.get_model('extensions', 'Extension')
|
||||
to_update = []
|
||||
for extension in Extension.objects.prefetch_related(
|
||||
'versions',
|
||||
'versions__file',
|
||||
@ -29,7 +30,8 @@ def populate_latest_version(apps, schema_editor):
|
||||
latest_version = version
|
||||
break
|
||||
extension.latest_version = latest_version
|
||||
Oleg-Komarov marked this conversation as resolved
|
||||
extension.save(update_fields={'latest_version'})
|
||||
to_update.append(extension)
|
||||
Extension.objects.bulk_update(to_update, ['latest_version'])
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
@ -409,7 +409,7 @@ class Extension(CreatedModifiedMixin, RatingMixin, TrackChangesMixin, models.Mod
|
||||
|
||||
@transaction.atomic
|
||||
def update_latest_version(self, skip_version=None):
|
||||
versions = self.versions.all().order_by('-date_created')
|
||||
versions = self.versions.select_related('file').all().order_by('-date_created')
|
||||
latest_version = None
|
||||
for version in versions:
|
||||
if skip_version and version == skip_version:
|
||||
@ -429,7 +429,7 @@ class Extension(CreatedModifiedMixin, RatingMixin, TrackChangesMixin, models.Mod
|
||||
and self.versions.filter(file__status=File.STATUSES.APPROVED).count() > 0
|
||||
)
|
||||
|
||||
# this method is called from post_save signal, this early return above should prevent a loop
|
||||
# this method is called from post_save signal, this early return should prevent a loop
|
||||
if self.is_listed == should_be_listed:
|
||||
return
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user
Extension.objects.bulk_update(.., fields={'latest_version'})