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):
|
def populate_latest_version(apps, schema_editor):
|
||||||
Extension = apps.get_model('extensions', 'Extension')
|
Extension = apps.get_model('extensions', 'Extension')
|
||||||
|
to_update = []
|
||||||
for extension in Extension.objects.prefetch_related(
|
for extension in Extension.objects.prefetch_related(
|
||||||
'versions',
|
'versions',
|
||||||
'versions__file',
|
'versions__file',
|
||||||
@ -29,7 +30,8 @@ def populate_latest_version(apps, schema_editor):
|
|||||||
latest_version = version
|
latest_version = version
|
||||||
break
|
break
|
||||||
extension.latest_version = latest_version
|
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):
|
class Migration(migrations.Migration):
|
||||||
|
@ -409,7 +409,7 @@ class Extension(CreatedModifiedMixin, RatingMixin, TrackChangesMixin, models.Mod
|
|||||||
|
|
||||||
@transaction.atomic
|
@transaction.atomic
|
||||||
def update_latest_version(self, skip_version=None):
|
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')
|
||||||
Oleg-Komarov marked this conversation as resolved
Outdated
Anna Sirota
commented
`select_related` missing for `.file`
|
|||||||
latest_version = None
|
latest_version = None
|
||||||
for version in versions:
|
for version in versions:
|
||||||
if skip_version and version == skip_version:
|
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
|
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
|
||||||
Oleg-Komarov marked this conversation as resolved
Outdated
Anna Sirota
commented
"below"? "below"?
|
|||||||
if self.is_listed == should_be_listed:
|
if self.is_listed == should_be_listed:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user
Extension.objects.bulk_update(.., fields={'latest_version'})