This file contains 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 models | |
class BulkGetOrCreateManager(models.Manager): | |
def bulk_get_or_create(self, objs, lookup_field=None): | |
assert lookup_field, "Not set 'lookup_field' for 'bulk_get_or_create'" | |
lookup = {f'{lookup_field}__in': objs} | |
existing_objects = [ | |
obj for obj in self.get_queryset().filter(**lookup) |
This file contains 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
# your_app/admin.py | |
# ... imports | |
@admin.register(User) | |
class UserAdmin(admin.ModelAdmin): | |
actions = ['broadcast'] # register method as action | |
def broadcast(self, request, queryset): | |
if 'apply' in request.POST: # if user pressed 'apply' on intermediate page |
This file contains 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
function slugify(text) | |
{ | |
return text.toString().toLowerCase() | |
.replace(/\s+/g, '-') // Replace spaces with - | |
.replace(/[^\w\-]+/g, '') // Remove all non-word chars | |
.replace(/\-\-+/g, '-') // Replace multiple - with single - | |
.replace(/^-+/, '') // Trim - from start of text | |
.replace(/-+$/, ''); // Trim - from end of text | |
} |