Snippet List
I want to create Mixins for QuerySet objects that will by default filter out certain records (e.g. filter out "deleted" records, or filter out "unapproved" records, etc). I'd like those to be separate independent mixins. So in each of those, I override all() to filter out deleted or unapproved, etc.
But, I also want to offer a method in the queryset to remove those filters or remove some part of those filters.
That's where this code comes in. After some examination of how QuerySets work, this seemed like the simplest method for "undoing" some filter in a queryset
- hack
- orm
- manager
- mixin
- queryset
This function mangles a generated form class to remove the Hold down "Control", or "Command"... messages from the help text. This is really a dirty hack awaiting a proper solution to [Django ticket 9321](http://code.djangoproject.com/ticket/9321).
This function can be useful for forms in the admin interface that use custom widgets. Basic usage:
class MyModelForm(forms.ModelForm):
class Meta:
model = MyModel
class MyAdmin(admin.ModelAdmin):
form = remove_holddown(MyModelForm, ('field1', 'field2'))
Very simple proof-of-concept that uses the django.forms library (newforms) for validation,
and formalchemy for saving the model instance using sqlalchemy.
it can be used like this (pseudo-code):
if form.is_valid():
form.save(session=Session, app_label='Contact')
Feel free to improve the concept. Ideally, either use formalchemy or django.forms
but not both like this example. ;-)
- newforms
- hack
- formalchemy
- notmm
- sqlalchemy
Example view code:
lazy_field_options = {
'field_name_that_is_m2m': {
'queryset': YourRelatedModel.objects.filter(groups=request.user.groups.all()),
},
'field_name_that_is_fk': {
'queryset': YourOtherRelatedModel.objects.filter(slug=request_slug),
},
}
modelform = YourModelForm(jpic_field_options=lazy_field_options)
# after the modelform has called for parent __init__, it will set
# options for each field if possible.
- hack
- form
- queryset
- modelchoicefield
- modelform
- modelmultiplechoicefield
It's cheap, hackish, and dirty, but it works. \\o/
Lines 19 & 20 are what you should edit.
6 snippets posted so far.