https://medium.com/@hakibenita/bullet-proofing-django-models-c080739be4e
This file contains hidden or 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
from django.core.management import BaseCommand | |
from apps.accounts.admin import UserResource | |
from django.contrib.auth import get_user_model | |
import time | |
class Command(BaseCommand): | |
# Show this when the user types help | |
help = "Export all User data" |
This file contains hidden or 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
$(document).ready(function() { | |
$(".expenses").on('keyup change', calculateSum); | |
}); | |
function calculateSum() { | |
var $input = $(this); | |
var $row = $input.closest('tr'); | |
var sum = 0; | |
$row.find(".expenses").each(function() { |
This file contains hidden or 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
sudo -u postgres psql | |
create role db_user with password 'db_pass'; | |
create database db_name with owner db_user; | |
alter role db_user WITH LOGIN; |
This file contains hidden or 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
pg_restore -U fmfb_user -h localhost --password -d fmfb_db < /home/sayone/Downloads/12sep18-dump |
This file contains hidden or 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
from django.db.models import OuterRef, Subquery | |
unique_users_query = (CouponApplication | |
.objects | |
.all() | |
.filter(coupon=OuterRef('id')) | |
.values('user_id') # group cities by state | |
.order_by() # reset ordering | |
.annotate(cnt=Count('*')) | |
.values('cnt')[:1] # Only have 1 row & 1 value allowed |
This file contains hidden or 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
pg_dump -U username -p 5433 -h localhost -F c database_name > dk_dump.dump |
This file contains hidden or 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
<!-- Download the js and css file and place at the below specified location. --> | |
<!-- In html add this to head tag --> | |
<link href="{% static 'css/toastr.min.css' %}" rel="stylesheet"> | |
<!-- In html add this just before end of body tag --> | |
<script src="{% static 'js/toastr.js' %}" type="text/javascript"></script> | |
This file contains hidden or 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
from django.db import connection | |
from django.http import HttpResponse | |
from django.utils.deprecation import MiddlewareMixin | |
# from django.utils.log import getLogger | |
# logger = getLogger(__name__) | |
class QueryCountDebugMiddleware(MiddlewareMixin): | |
""" | |
This middleware will log the number of queries run |
This file contains hidden or 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
from django.db.models import QuerySet | |
from django.views import View | |
class DataTableMixin(View): | |
queryset = None | |
columns = None | |
serializer_class = None | |
page_size = 10 | |
def get_queryset(self): |