When saving, how can you check if a field has changed?

Essentially, you want to override the __init__ method of models.Model so that you keep a copy of the original value. This makes it so that you don’t have to do another DB lookup (which is always a good thing). class Person(models.Model): name = models.CharField() __original_name = None def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.__original_name = … Read more

Capturing URL parameters in request.GET

When a URL is like domain/search/?q=haha, you would use request.GET.get(‘q’, ”). q is the parameter you want, and ” is the default value if q isn’t found. However, if you are instead just configuring your URLconf**, then your captures from the regex are passed to the function as arguments (or named arguments). Such as: (r’^user/(?P<username>\w{0,50})/$’, … Read more

In a Django form, how do I make a field readonly (or disabled) so that it cannot be edited?

As pointed out in this answer, Django 1.9 added the Field.disabled attribute: The disabled boolean argument, when set to True, disables a form field using the disabled HTML attribute so that it won’t be editable by users. Even if a user tampers with the field’s value submitted to the server, it will be ignored in … Read more

Dynamically adding a form to a Django formset

This is how I do it, using jQuery: My template: <h3>My Services</h3> {{ serviceFormset.management_form }} {% for form in serviceFormset.forms %} <div class=”table”> <table class=”no_error”> {{ form.as_table }} </table> </div> {% endfor %} <input type=”button” value=”Add More” id=”add_more”> <script> $(‘#add_more’).click(function() { cloneMore(‘div.table:last’, ‘service’); }); </script> In a javascript file: function cloneMore(selector, type) { var newElement … Read more

How to upload a file in Django? [closed]

Phew, Django documentation really does not have good example about this. I spent over 2 hours to dig up all the pieces to understand how this works. With that knowledge I implemented a project that makes possible to upload files and show them as list. To download source for the project, visit https://github.com/axelpale/minimal-django-file-upload-example or clone … Read more

Extending the User model with custom fields in Django

The least painful and indeed Django-recommended way of doing this is through a OneToOneField(User) property. Extending the existing User model … If you wish to store information related to User, you can use a one-to-one relationship to a model containing the fields for additional information. This one-to-one model is often called a profile model, as … Read more

How can I combine two or more querysets in a Django view?

Concatenating the querysets into a list is the simplest approach. If the database will be hit for all querysets anyway (e.g. because the result needs to be sorted), this won’t add further cost. from itertools import chain result_list = list(chain(page_list, article_list, post_list)) Using itertools.chain is faster than looping each list and appending elements one by … Read more

tech