Assuming it quacks like a duck, you can use operator.attrgetter()
Combine queryset by turning it into a list:
queryset_list = list(queryset1).extend(list(queryset2))
Sort the list by:
sorted_list = queryset_list.sort(key=operator.attrgetter(‘attribute to sort by’))
Categories: Life
Quickly style all of your inlines with formfield_for_dbfield
class PersonInline(admin.TabularInline):
model = Person
extra = 6
def formfield_for_dbfield(self, db_field, **kwargs):
attrs = { 'size': 15 }
if db_field.attname == 'interest_level':
attrs = { 'size': 2 }
kwargs['widget'] = forms.TextInput(attrs=attrs)
return super(PersonInline, self).formfield_for_dbfield(db_field,**kwargs)
I see this used for things like custom widgets, ReadOnlyWidget, ColoredWidget, etc, but I wanted a quick and dirty(?) way to fit more fields on my inlines, since the admin just overflows.
Categories: Life
This is documented:
To modify a query based on user, override the ‘queryset’ for ModelAdmin like so. Should give enough ideas:
def queryset(self, request):
qs = super(PanelTasting, self).queryset(request)
if request.user.is_superuser:
return qs
return qs.filter(host__user=request.user)
Categories: Life
Since so much admin functionality has moved to AdminSite, I couldn’t find the answer to this very easily on google searches.
Turns out the names the admin site uses to display model names is still defined in the models.Model class.
Add class Meta:
verbose_name = “blah”
verbose_name_plural = “blahs”
to avoid names like Entrys.
Sure it’s model specific, but given the option to create multiple AdminSite instances these things now become AdminSite specific.
Categories: Life
September 12, 2009 · 2 Comments
I couldn’t figure out what to title this post, but this victory felt sweet.
I wanted to make a searchable, sorted list of an arbitrary amount of fields to check against.
Normally, we use a Q object to get to the | “or” operator.
Person.objects.filter( Q(first_name__icontains='Yuji') | Q(last_name__icontains='Tomita')
But I wanted to use dynamic field names so that I can use this same ‘factory’ on other models.
With much needed prodding in the right direction by mattmcc, I figured it out.
The much needed key was how to use keyword arguments:
Person.objects.filter(first_name__icontains=’Yuji’)
is equal to:
Person.objects.filter( **{‘first_name__icontains’:'Yuji’})
Therefore, I could create as many Q objects as I wanted in a list.
q_list = []
for q in query_string.split(‘ ‘):
for field in fields:
q_list.append( Q(**{field+’__icontains’: q})
query = model.objects.filter( reduce(operator.or_, q_list)
Bam!
Categories: Life
PYTHONPATH is set on windows in the registry.
Run regedit (start -> run -> regedit ) and browse to HKEY_LOCAL_MACHINE -> SOFTWARE -> Python -> PythonCore -> [ Version Number ] -> PythonPath.
Don’t alter the pythonpath you see here.
Right click the PythonPath folder and select New –> Key
Name it what you like.
Select the new folder and right click the one blank value sitting on the right side of the screen and select Modify.
For the value field, add your new paths separated by ” ; “
Run python and try an import.
Done!
Categories: Life
To set the PATH, on Vista and newer (windows 7), push the windows button and type environment variable.
Select “Edit environment variables for your account”
Non vista+ users just right click on My Computer, go to Properties, click Advanced Properties, then the Environment Variables button to get to the same place.
Here, edit your PATH variable under System Variables.
Append whatever you like to the string ending with a ” ; “
BLAH;Blah;blah;
If you have a script hello_world.py in C:\bin, adding C:\bin to your PATH will allow you to run hello_world.py from anywhere.
Categories: Life
I just used the mimetypes python standard library to guess filetypes via file names.
It assumes the user followed naming conventions and does not attempt to read the data.
Categories: Life
Tagged: Django, MIME Type, Python
I didn’t really know that you escape a % in a string by itself, not \ or anything.
So there you have it..
"1%% %s" % "hello"
returns
1% hello
Categories: Life
December 19, 2008 · 1 Comment
#!admin.py
from django.contrib.admin.sites import AdminSite
my_new_admin = AdminSite()
#!urls.py
from myproject.admin import my_new_admin
(r'^my_new_admin/(*.)', my_new_admin.root)
Categories: Life