Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Figure out why tune_play view doesn't update last_played field in database #58

Open
jwjacobson opened this issue Dec 6, 2023 · 4 comments
Assignees

Comments

@jwjacobson
Copy link
Owner

Everything looks good in this view but the last_played field remains null

@bbelderbos
Copy link
Collaborator

Seems this attribute is on the m2m table RepertoireTune

Yet the object your setting it on is Tune:

 72  	    breakpoint()
 73  ->	    tune_to_play.last_played = datetime.now()
 74  	    tune_to_play.save()
 75
 76  	    return render(request, "tune/play.html", {"tune_to_play": tune_to_play})
[EOF]
(Pdb) tune_to_play
<Tune: Tune 2 | abc>
(Pdb) datetime.now()
datetime.datetime(2023, 12, 6, 17, 1, 21, 525464)

That's where Python's permissiveness sometimes bites us, this should have raised an error ideally, yet it was "just ok" to do so ...

Makes sense?

@bbelderbos
Copy link
Collaborator

bbelderbos commented Dec 6, 2023

rep_tunes = RepertoireTune.objects.filter(player=user)
tunes = [rep_tune.rep_tune for rep_tune in rep_tunes]

1st statement starts well, but in the listcomp of the 2nd statement you switch to tunes:

(Pdb) rep_tunes
<QuerySet [<RepertoireTune: RepertoireTune object (1)>]>
(Pdb) [rep_tune.rep_tune for rep_tune in rep_tunes]
[<Tune: Tune 2 | abc>]

You can also do this in one step, using the ORM only:

(Pdb) rep_tune_to_play = RepertoireTune.objects.filter(player=user).order_by('?').first()
(Pdb) rep_tune_to_play
<RepertoireTune: RepertoireTune object (1)>
  • .order_by('?'): Orders the resulting queryset randomly. The ? is a special character in Django's ORM that signifies random ordering.
  • .first(): Gets the first element of the queryset. Since the queryset is ordered randomly, this effectively selects a random instance.

@bbelderbos
Copy link
Collaborator

There is another issue to address:

pdb cont'd

(Pdb) rep_tune_to_play.last_played = datetime.now()
(Pdb) rep_tune_to_play.save()
/Users/bbelderbos/code/PDM/projects/jeffj/jazz_repertoire/venv/lib/python3.12/site-packages/django/db/models/fields/__init__.py:1595: RuntimeWarning: DateTimeField RepertoireTune.last_played received a naive datetime (2023-12-06 17:07:14.529728) while time zone support is active.
  warnings.warn(

You should use timezone to work around this:

(Pdb) from django.utils import timezone
(Pdb) rep_tune_to_play.last_played = timezone.now()
(Pdb) rep_tune_to_play.save()
(Pdb) rpt = RepertoireTune.objects.last()  # I only have one in my db right now
(Pdb) rpt.last_played
datetime.datetime(2023, 12, 6, 17, 8, 24, 951509, tzinfo=datetime.timezone.utc)

@bbelderbos bbelderbos mentioned this issue Dec 6, 2023
@bbelderbos
Copy link
Collaborator

Also confirming this works by looking in django admin:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants