What is the best way to access stored procedures in Django’s ORM

We (musicpictures.com / eviscape.com) wrote that django snippet but its not the whole story (actually that code was only tested on Oracle at that time). Stored procedures make sense when you want to reuse tried and tested SP code or where one SP call will be faster than multiple calls to the database – or … Read more

Override a form in Django admin

You can override forms for django’s built-in admin by setting form attribute of ModelAdmin to your own form class. See: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.form https://docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-custom-validation-to-the-admin It’s also possible to override form template – have a look at https://docs.djangoproject.com/en/dev/ref/contrib/admin/#custom-template-options If you’re looking specifically for autocomplete I can recommend https://github.com/crucialfelix/django-ajax-selects

Django admin – inline inlines (or, three model editing at once)

You need to create a custom form and template for the LinkSectionInline. Something like this should work for the form: LinkFormset = forms.modelformset_factory(Link) class LinkSectionForm(forms.ModelForm): def __init__(self, **kwargs): super(LinkSectionForm, self).__init__(**kwargs) self.link_formset = LinkFormset(instance=self.instance, data=self.data or None, prefix=self.prefix) def is_valid(self): return (super(LinkSectionForm, self).is_valid() and self.link_formset.is_valid()) def save(self, commit=True): # Supporting commit=False is another can of worms. … Read more

Can Django automatically create a related one-to-one model?

Take a look at the AutoOneToOneField in django-annoying. From the docs: from annoying.fields import AutoOneToOneField class MyProfile(models.Model): user = AutoOneToOneField(User, primary_key=True) home_page = models.URLField(max_length=255) icq = models.CharField(max_length=255) (django-annoying is a great little library that includes gems like the render_to decorator and the get_object_or_None and get_config functions)

update django database to reflect changes in existing models

As of Django 1.7+, built-in migrations support, allows for database schema migrations that preserve data. That’s probably a better approach than the solution below. Another option, not requiring additional apps, is to use the built in manage.py functions to export your data, clear the database and restore the exported data. The methods below will update … Read more

Django: How to create a model dynamically just for testing

You can put your tests in a tests/ subdirectory of the app (rather than a tests.py file), and include a tests/models.py with the test-only models. Then provide a test-running script (example) that includes your tests/ “app” in INSTALLED_APPS. (This doesn’t work when running app tests from a real project, which won’t have the tests app … Read more

tech