Django & Python Blog

Understanding Django Migrations

Models allow us to define the structure, relations, and behavior of the data we use on our website. The behavior we’ve defined will have a direct effect on our use of the data in Python (as we shall see in the next section), but the structure we’ve created is just an abstraction for how the data will be stored in the database. In Django, models and the database schema are reflections of one other. Any modification of one must result in the modification of the other. In a fully deployed team project, this can be quite tricky. If one developer makes a change to his or her local test database, he or she needs a way to share these changes with other developers and the various servers running the website(s) in a repeatable and version-controlled manner. If the change turns out to be in error, the developer should also have an easy way to undo his or her changes. Finally, if two developers are working on the same area of the code and make conflicting changes, it should be easy to recognize the issue and resolve the problem.

Migrations solve these problems by providing a controlled, predictable system for altering a database. The typical workflow with Django is to 1. Create or change a model in Django, 2. Generate a migration file, 3. Use the migration file to create/alter the database.
The migration file contains the instructions to alter the database and to roll back (or revert) those alterations. The advantage of keeping these instructions in a file is that this file may be easily run (applied) and shared among developers. Migration files may be considered version control for the database, although the analogy is imperfect. The advantage of having these changes as files (and where the analogy breaks down) is the ability to store and share these files using a standard version-control system.

Migrations are a key part of every Django project, as they help avert major headaches and save time.

jQuery and Ajax Integration in Django

We will be working with the leading Python web framework, Django, on the server side, and jQuery-powered Ajax on the client side. During the course of this post, we will cover the basic technologies and then see them come together in an employee intranet photo directory that shares some Web 2.0 strengths.

There is more than one good JavaScript library; we will be working with jQuery, which has reached acceptance as a standard lightweight JavaScript library. It might be suggested that Pythonistas may and much to like in jQuery: jQuery, like Python, was carefully designed to enable the developer to get powerful results easily.

Human speech: An overlaid function

Human speech is an overlaid function. What is meant by this is rejected in the answer to a question: “What part of the human body has the basic job of speech?” The tongue, for one answer, is used in speech, but it also tastes food and helps us swallow. The lungs and diaphragm, for another answer, perform the essential task of breathing. The brain cannot be overlooked, but it also does a great many other jobs. All of these parts of the body do something more essential than speech and, for that matter, all of these can be found among animals that cannot talk. Speech
is something that is overlaid over organs that are there in the first place because of something other than speech.
Something similar to this is true for Ajax, which is not a technology in itself,
but something overlaid on top of other technologies. Ajax, some people say,
stands for Asynchronous JavaScript and XML, but that was a retroactive expansion. JavaScript was introduced almost a decade before people began seriously talking about Ajax. Not only is it technically possible to use Ajax without JavaScript (one can substitute VBScript at the expense of browser compatibility), but there are
quite a few substantial reasons to use JavaScript Object Notation (JSON) in lieu of heavy-on-the-wire eXtensible Markup Language (XML). Performing the overlaid function of Ajax with JSON replacing XML is just as eligible to be considered full- edged Ajax as a solution incorporating XML.

Ajax: Another overlaid function

Ajax is a way of using client-side technologies to talk with a server and perform partial page updates. Updates may be to all or part of the page, or simply to data handled behind the scenes. It is an alternative to the older paradigm of having a whole page replaced by a new page loaded when someone clicks on a link or submits a form. Partial
page updates, in Ajax, are associated with Web 2.0, while whole page updates are associated with Web 1.0; it is important to note that “Web 2.0” and “Ajax” are not interchangeable. Web 2.0 includes more decentralized control and contributions besides Ajax, and for some objectives it may make perfect sense to develop an e-commerce site that uses Ajax but does not open the door to the same kind of community contributions as Web 2.0.