Django – Running makemigration does not detect changes in models.py

To fix this issue, refer to the app name when running the code

 

python manage.py makemigrations name_of_app

The correct app name can be found from the app.py file

Advertisements

Tkinter TreeView – Enable Sorting upon Clicking Column Headings

The below code snippet can help you achieve the Sorting Option on Treeview Column Headings in Tkinter in both ascending and descending order.

columns = ("Student","Course","Last Session")

tv = Treeview(popup, show='headings', columns=columns,height=25)

def treeview_sort_column(tv, col, reverse):
    l = [(tv.set(k, col), k) for k in tv.get_children('')]
    l.sort(reverse=reverse)

    # rearrange items in sorted positions
    for index, (val, k) in enumerate(l):
        tv.move(k, '', index)

    # reverse sort next time
    tv.heading(col, command=lambda _col=col: treeview_sort_column(tv, _col, not reverse))

for col in columns:
    tv.heading(col, text=col,command=lambda _col=col: treeview_sort_column(tv, _col, False))
tv.pack()

 

ModuleNotFoundError: No module named ‘babel.numbers’

Traceback (most recent call last):
File "tkinter/__init__.py", line 1705, in __call__
File "script.py", line 726, in
File "script.py", line 536, in calendar
File "tkcalendar/calendar_.py", line 211, in __init__
File "babel/dates.py", line 314, in get_day_names
File "babel/core.py", line 641, in days
File "babel/core.py", line 364, in _data
File "babel/localedata.py", line 111, in load
File "babel/localedata.py", line 111, in load
File "babel/localedata.py", line 117, in load
ModuleNotFoundError: No module named 'babel.numbers'

Solution Explained

Locate the tkcalendar folder under venv/lib/python3.x/site-packages of your project folder.

Under calendar.py, add an additional import for the missing module.

import calendar
from babel.dates import format_date, parse_date, get_day_names, get_month_names
from babel.numbers import *  # Additional Import

Mac OS X – ValueError: unknown locale: UTF-8

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/locale.py", line 568, in getdefaultlocale
return _parse_localename(localename)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/locale.py", line 495, in _parse_localename
raise ValueError('unknown locale: %s' % localename)
ValueError: unknown locale: UTF-8

To Solve the above issue you will need to add the below variables in your .profile or .bash_profile

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

From the Launchpad, open the Terminal

Type nano .profile
Nano is an easy to use editor

Copy the below lines of code
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

Press Ctrl+O to save the lines
Press Return to Save
Press Ctrl+X to Exit

To verify that the variables have been added to the .profile, use the below code in the terminal

open -e .profile

Try running your program once again. If the issue has not been resolved, use the same approach with the file .bash_profile

 

gspread timeout – Reconnecting to the Google API

gspread loses connection with the Google API probably every 10 minutes. This can be easily tackled by calling a function or Class which reconnects to the database.

import gspread
from oauth2client.service_account import ServiceAccountCredentials

from datetime import datetime
from datetime import timedelta

def connect_database():

scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)

workbook1 = client.open("File_Name")
w1 = workbook1.worksheet("Sheet1")
w2 = workbook1.worksheet("Sheet2")

return w1,w2

program_start_time=datetime.now()

if datetime.now() > program_start_time + timedelta(minutes=10):
worksheet1, worksheet2 = connect_database()
program_start_time = datetime.now() #Resets the Program Start Time

gspread – Add Formula to Google Spreadsheet Cells

gspread documentation does not have any examples of adding formulas to the cells. If you try to use them with append_row or update_cell methods it records them as a string and not as a Formula.

You should instead use the update_cell method

Updating a Single Cell, considering you know the exact Row and Column Number

worksheet2.update_acell('A45','=SUM(D45:F45)')

Updating a Single Cell, considering you the exact Row and Column Number are not known

cell = worksheet2.find("David Letterman")
row_number = cell.row
workbook2_row='A'+str(row_number)

worksheet2.update_acell(workbook2_row,'=INDIRECT(CONCATENATE("D",ROW()))-INDIRECT(CONCATENATE("E",ROW()))')