-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #311 from domino14/feature/308/new_lexica
fix an auto_now issue with SavedList and the migration
- Loading branch information
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
""" | ||
I made a mistake migrating all lists to new lexicon as it updates | ||
the saved date on these. Bring them back to what they used to be. | ||
""" | ||
import pickle | ||
|
||
from django.core.management.base import BaseCommand, CommandError | ||
from base.models import WordList | ||
|
||
|
||
def turn_off_auto_now(ModelClass, field_name): | ||
def auto_now_off(field): | ||
field.auto_now = False | ||
do_to_model(ModelClass, field_name, auto_now_off) | ||
|
||
|
||
def do_to_model(ModelClass, field_name, func): | ||
field = ModelClass._meta.get_field(field_name) | ||
func(field) | ||
|
||
|
||
class Command(BaseCommand): | ||
def add_arguments(self, parser): | ||
parser.add_argument('pickle_file', type=str) | ||
|
||
def handle(self, *args, **options): | ||
if 'pickle_file' not in options: | ||
raise CommandError('You must pass in pickle_file with old dates') | ||
|
||
dates_obj = pickle.load(open(options['pickle_file'], 'rb')) | ||
|
||
turn_off_auto_now(WordList, 'lastSaved') | ||
|
||
for list_id, last_saved in dates_obj.items(): | ||
try: | ||
wl = WordList.objects.get(pk=list_id) | ||
except WordList.DoesNotExist: | ||
print(f'word list with id {list_id} no longer exists') | ||
continue | ||
|
||
wl.lastSaved = last_saved | ||
wl.save() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters