# given a list of ints this will get you the index of the largest value
idx = max(range(len(A)), key=A.__getitem__)
Django: get_FOO_display seems cool
get_FOO_display from django.db import models class Person(models.Model): SHIRT_SIZES = ( ('S', 'Small'), ('M', 'Medium'), ('L', 'Large'), ) name = models.CharField(max_length=60) shirt_size = models.CharField(max_length=2, choices=SHIRT_SIZES)
>>> p = Person(name="Fred Flintstone", shirt_size="L") >>> p.save() >>> p.shirt_size 'L' >>> p.get_shirt_size_display() 'Large'
Django: refresh_from_db
Need to refetch a model from db? # DO THIS obj.refresh_from_db()# INSTEAD OF updated_obj = Model.objects.get(pk=obj.pk)
Problems? Delete virtual environment and remake it.
Just a note for later. Trust me, it makes sense.
Matt’s Tips
avoid doing this…
- Do not do “things” in an
__init__
- Do not assign something to
self
when you can instead make it a@property
or could pass it as an argument.
helpful vim commands
map
help
could not connect to server: Connection refused
psycopg2.OperationalError: could not connect to server: Connection refused
Is the server running on host “127.0.0.1” and accepting
TCP/IP connections on port 57359?
Ran into this error at work. It turns out the user had deleted their known_hosts file. All that was required to fix it was to tunnel out to the ec2 instance and accept that ssh address.
Prod shell activate venv
source /opt/python/current/env
source /opt/python/run/venv/bin/activate
cd /opt/python/current/app
python manage.py showmigrations
(V)im (U)pdate
space + V + U
Updates vim plugins. Used it to fix a fuzzy find issue I was having.
space p
Allows you to paste from a register that stored what you copied (not what you may have inadvertently copied by deleting something).