Django for the Rails Developer

Shabda Raaj
Shabda Raaj

Django is the most popular Python web application development framework. Many people consider it Python’s Rails. If you are an experienced Rails developer this article will help you get started with building Django apps and help map Rails concepts to Django.

What Do I Need to Get Started?

We will assume that you are an experienced developer and so spend no time in explaining the basics such as installing Django or Python. If you need help with that, please look here. We will also not focus on creating a basic app using Django. Django docs already cover that, if you need help read this article.

If you are still reading, I assume you can build a basic Django app, and you now want to map your Rails concepts to Django. Lets get started.

You Say MVC, I Say MTV

Rails is an MVC framework. You write your data interactions in app/models/model_name.rb, you write your controllers in app/controllers/controller_name.rb and you render the data using a view from app/views/. Django is a MVC framework, but it differs from standard MVC terminology.

In Django, the controllers become the views, and views become the template.

So in

Rails: Controller is the ruby code which decides what data gets shown.
Django: View is the Python code which determines what data gets shown.


Rails: Views decide how the data is rendered. This can be erb or haml code.
Django: Templates decide how the data is rendered. This is html with Django specific tags.

Django calls this the MTV architecture and has more details about this in their faqs.

At Least the Models Are the Same?

You would hope so, but then life wouldn’t be so much fun. In Rails db/schema.rb is the authoritative source for your database schema. To add a new model or to make changes to your existing model, you create and run a migration which changes the schema. The schema.rb file is auto-generated from the current state of the database. You would generally not edit the schema.rb manually. You keep your model validations and business logic in app/models/model_name.rb.

In Django, the authoritative source of thie database schema is app_name/ To create a new table, you edit your and then run either the syncdb or the migrate command. You will need the very popular south app for migrate command.

In Rails, the DB is the authoritative source and schema.rb is autogenerated from it. In Django, is the authoritative source and DB is synced from it.

Not in a Complicated Relationship

In Rails you would model foreign key relationships via has_many, belongs_to, has_one, has_and_belongs_to_many. In Django you specify them as attributes on your models as models.ForeignKey, models.OneToOneField, models.ManyToManyField. In Django you only need to specify one side of the relationship, the other Model object will automatically get the related methods. Here is the mapping.

belongs_to -> ForeignKey
has_many -> (Not needed, as Django automatically add these after getting a ForeignKey)
has_one -> OneToOneField (Only needed to specify on one of the models)
has_and_belong_to_many -> ManyToManyField (Only needed to specify on one of the models)

Model Objects and Querysets.

Once you have defined the model, they have many equivalent methods. Lets say you have a User model available in both Rails and Django.

Rails: User.create(..)
Django: User.objects.create(..)

Saving a model after making some changes


Django queryset and Rails AR relations are also pretty much equivalent, offering similar methods.

Rails: Model.find(10)
Django: Model.objects.get(id=10)

Rails: Model.where(..conditions)
Django: Model.objects.filter(..conditions)

Rails: Model.order(“createdat”)
Django: Model.objects.orderby(“created_at”)

The big difference is that, in Django, the methods are on a manager. (Generally called objects), instead of the Model class itself.

Routing and Config

In Rails, the config files are in config/* and each environment, such as development, test, and production, get their own config file. In Django most of the configuration is in Rails puts routing in config/routes.rb. Django puts rouring/dispatch in and is regex based.

Templating and Views

Rails views are ERB or haml files. Django templates have template tags, which correspond to Rails helpers and the Django {% include ..%} template tag corresponds to Rails partials.

Some More Mappings.

Development Server

Rails: rails server
Django: python server

Development Console

Rails: rails console
Django: python shell

Database Migrations

Rails: rake db:create && rake db:migrate
Django: python syncdb && python migrate


Django comes with a script which performs many actions available on rails/rake.


Rails: config/routes.rb


Rails: callbacks (such as beforesave)
Django: signals (Such as presave) or overriding lifecycle methods such as .save


Rails: observers
Django: signals

Custom Tasks

Rails: custom rake tasks
Django: Custom management commands

What Next?

This article should give you enough pointers to apply your Rails knowledge to start working on Django. This article is specifically not a Rails vs Django post. Rails and Django are both amazing frameworks, and knowing Django can give you valuable insights and ideas to work better with Rails even if you never use Django in production.

I’d love to hear your take on the similarities and differences between Rails and Django in the comments section. Thanks for reading!