Change model field attribute in a DRY way
Change model field attributes from a abstract base classes model in a dry way.
- model
- field
- dry
Change model field attributes from a abstract base classes model in a dry way.
**Your model:** class TicketItem(models.Model): hours = models.DecimalField(decimal_places=2, max_digits=6) day = models.DateField() order = models.ForeignKey(Order) tickets = models.ManyToManyField(Ticket) Now you want to auto save m2m fields in your forms.TicketItemCreateForm: 1. inherit from m2mForm-Class 2. define m2m_field(s) **Example:** class TicketItemCreateForm(m2mForm): m2m_field = 'tickets' class Meta: model = models.TicketItem
Renders enclosing contents as it is. Useful to avoid tags conflict with certain javascript libraries (eg. jquery-tmpl.js, mustache.js) Example usage: `<script class="content-tmpl" type="text/x-jquery-tmpl">` `{% alien %}` `{{ tmpl(results) "#results .item-tmpl" }}` `{% endalien %}` `</script>`
This snippet is an improved version of the [ifusergroup](http://djangosnippets.org/snippets/1576/) tag that allows spaces in any of the group names. It also fixes a small bug where if a group didn't exist none of the subsequent groups would be checked.
Here's the python code to produce an admin related widget with the edit and delete link also. * [RelatedFieldWidgetWrapper](http://djangosnippets.org/snippets/2562) * [related-widget-wrapper.html](http://djangosnippets.org/snippets/2563) * [related-widget-wrapper.js](http://djangosnippets.org/snippets/2564) * [RelatedWidgetWrapperAdmin](http://djangosnippets.org/snippets/2565)
Ping ICMP ip with jquery.
This math captcha field and widget was inspired by Justin Quick's django-math-captcha, but I wanted to make it into one form field and not have anything in settings.py. I removed the division and modulo operators to avoid people having to input fractions, and just randomly select the operator. It leverages Django's built-in MultiValueField to set up the hidden hashed answer to compare the user's input to instead of rendering strings for the fields like django-math-captcha does. Unit tests soon to follow, but this is used in production at: http://btaylorweb.com/. Enjoy!
I have been wrestling with custom app labels and have come to the conclusion that this is the easiest workaround for the problem. Since I name most of my apps as Foo_Bar this makes them look a lot nicer. I just apply the filter to whatever app label and it fixes it. If you need a more complex replacement, you could just have an if statement that looks for the full app label and replaces it with whatever you want.
Django Template Tag Filter stack to clean up output from [widgEditor](http://www.themaninblue.com/experiment/widgEditor/) or other WYSIWYG text-input box. Removes arbitrary line break code and replaces with Django's cleaner `|linebreaks` filter. Also removes any arbitrary styling, leaving in things like bold, italic, link and image tags.
You can use this function to change an admin option dynamically. For example, you can add a custom callable to *list_display* based on request, or if the current user has required permissions, as in the example below: class MyAdmin(admin.ModelAdmin): list_display = ('__unicode__', 'other_field') def changelist_view(self, request, extra_context=None): if request.user.is_superuser: add_dynamic_value(self, 'list_display', my_custom_callable) return super(MyAdmin, self).changelist_view(request, extra_context)
If you want to test for trivial error in your add and changelist admin views, use this snippet. Save the snippet in admintests.py and put it anywhere in your pythonpath. Put this code in your tests.py: from django.test import TestCase from admintest import adminviews_test class TestAdminViews(TestCase): def test_admin_views(self): adminviews_test(self)
Look here: https://bitbucket.org/depaolim/optlock The snippet of Marco De Paoli is much better than this one! :-) ========================================================= If two users save same record, the second one will overwrite first one. Use this snippet to achieve an optimistic locking, so the second user will get an exception. Save the snippet in optlock.py and put it anywhere in your pythonpath. **Do not put _version_opt_lock in readonly_fields** or the snippet will fail! (if you need to hide it, use jquery). The snippet need in request.POST the original value of _version_opt_lock and if you make it a readonly field Django doesn't POST its value. When this [bug](https://code.djangoproject.com/ticket/11277) will be fixed it should be possible to use a hiddeninput widget. models.py example: ... import optlock ... class YourModel(optlock.Model): ... admin.py example: ... import optlock ... class YourModelAdmin(optlock.ModelAdmin): ... That's it :-)
**Description** A filestorage system that + is whitlisted, + changes the file name and targeting directory to put the file in - with respect to (runtime) instance information. + replaces files if they exists with the same name. Kudos to [jedie](http://djangosnippets.org/users/jedie/) - http://djangosnippets.org/snippets/977/
This allows you to order the models on the index page of the administration site in a custom way. This modification goes in the index method of django.contrib.admin.sites.AdminSite. I personally monkey patched it in where my models are loaded and registered with the admin site. Be careful that if you add new models you update the sorting dictionary, else you will get a key error. If no sorting is defined for an app, it will default to alphabetical order. Note that you'll probably want to also insert this into the app_index function as well. --- If you like my work, please check out my employer's site at 829llc.com - Dan
TaskViewMixin can be mixed in with a Class Based view and handles the scheduling and polling of a task. During task execution, a waiting page is rendered that should refresh itself. Once the task is complete, the view may then render a success page and can collect the payload of the task for rendering the result.
3110 snippets posted so far.