Login

Tag "hack"

Snippet List

Remove a clause from a queryset

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
Read More

Manage.py alterdb command

added commands: altersql - shows sql code with alter queries alterdb - apply alter queries. parameters: --showsql - show queries --app=APPLICATION - alter only selected application [you need clone this repo](https://bitbucket.org/certator/django_snippets)

  • hack
  • table
  • syncdb
  • alter
Read More

remove the annoying "Hold down control..." messages

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'))

  • admin
  • forms
  • hack
  • widget
Read More

Formalchemy hack for newforms-based form validation

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
Read More

Lazy options on ModelForm fields - like setting a ModelChoiceField queryset from the view

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
Read More

6 snippets posted so far.