Skip to content

Instantly share code, notes, and snippets.

View nara-l's full-sized avatar

Lawrence Nara nara-l

View GitHub Profile
@nara-l
nara-l / name_migration_files
Created November 30, 2018 11:41
Name django migration files
# To add a name to migration files rather than 001_auto_20189084774
python manage.py makemigrations --name DESIRED_NAME appName
# e.g. if app name is `site` and you want to name migration, `new_side_bar`
python manage.py makemigrations --name new_side_bar site
@nara-l
nara-l / get_model_object_django
Last active November 1, 2018 16:38
How to get a model object from instance in Django
# Sometimes you need to determine a model name from an object, before using
# First long approach may work in every version of Django? not tested
import sys
model_obj # let model_obj be the model object ( instance) from some input
model_name_str = type(model_obj).__name__ # this is model name as string, but we need the object to do the
model_object = getattr(sys.modules[__name__], model_name_str)
# now we can use model object normally
@nara-l
nara-l / reverse_lookup_django
Created October 25, 2018 21:31
Django What is reverse relationship?
`class Blog(models.Model):
#some attributes
class Entry(models.Model):
blog = models.ForeignKey(Group, related_name='entries') . # this is a ForeignKey relationship, not a OneToOneField
#more attributes
`
@nara-l
nara-l / Heroku_Monthly_Scheduler
Created September 26, 2018 15:58
Heroku Monthly Scheduler
# funny thing Heroku scheduler has no way of running a task monthly easily, one has to use a command like this:
if [ "$(date +%d)" = 01 ]; then MY_COMMAND; fi
# Run task every monday
if [ "$(date +%u)" = 1 ]; then MY_COMMAND; fi
# Run task every 24th of the year
if [ "$(date +%m)" = 12 ] && [ "$(date +%d)" = 24 ]; then MY_COMMAND; fi
@nara-l
nara-l / Fixtures - with Tenant Command Django
Created September 20, 2018 03:03
Django Fixtures for with tenant_command
python manage.py tenant_command dumpdata --schema="schema-name" app.personalitytypes > app/fixtures/data.json
# 'app' and 'schema-name' to be replaced with necessary variables
@nara-l
nara-l / gist:2dcd9c569d0ca484a64db24d0c38e6ea
Created August 28, 2018 19:47
Tenary Operator Python 2.7 ( since 2.5 )
# tenary opeartor
foo = test? True: False
# python equivalent with if else on same line
foo = "True" if test else "False"
@nara-l
nara-l / update_recent_commit_message
Created August 17, 2018 01:27
Change recently git committed message after pushing to github
git commit --amend -m "New commit message"
# then
git push --force
# it creates a new message and creates a merge of the branch with itself it appears
# this works assuming no one else did any push
@nara-l
nara-l / sort_array_of_objects_javascript
Created August 16, 2018 14:53
Javascript better sort Array of Objects
function dynamicSort(property) {
var sortOrder = 1;
if(property[0] === "-") {
sortOrder = -1;
property = property.substr(1);
}
return function (a,b) {
var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
return result * sortOrder;
}
@nara-l
nara-l / update_config_vars_heroku
Created July 24, 2018 15:47
Looking to update .env file on heroku by setting config vars
heroku config:set VAR_TO_BE_UPDATED: var_value --app APPNAME
# VAR_TO_BE_UDPATED : var_value is the key value pair of your .env variable
# APPNAME is the name of the app you want to update
@nara-l
nara-l / adding_days_to_fields_use_interval
Created July 20, 2018 15:22
Using interval to add days to date time field Posgres
# say you want to extend the expiration date for subscription service to 14 days, in postgres you can do that by
update users set expiration_date = expiration_date + interval '14 days'; # users is the table where expiration is stored
# I bet this could work, not tested
update users set expiration_date += interval '14 days';
# you could do so much more with interval > http://www.postgresqltutorial.com/postgresql-interval/