Saturday, October 27, 2012

Following relationships 'backwards'

Django lets you easily follow a ForiegnKey relationship backwards from an instance of the the referenced model back to the model containing the FK field.

The docs explain this well..

If a model has a ForeignKey, instances of the foreign-key model will have access to a Manager that returns all instances of the first model. By default, this Manager is named FOO_set, where FOO is the source model name, lowercased. This Manager returns QuerySets, which can be filtered and manipulated as described in the "Retrieving objects" section above.

Example:
>>> b = Blog.objects.get(id=1)
>>> b.entry_set.all() # Returns all Entry objects related to Blog.
# b.entry_set is a Manager that returns QuerySets.
>>> b.entry_set.filter(headline__contains='Lennon')
>>> b.entry_set.count()

The docs also go on to explain how and why these relations are possible..

Other object-relational mappers require you to define relationships on both sides. The Django developers believe this is a violation of the DRY (Don't Repeat Yourself) principle, so Django only requires you to define the relationship on one end.
The first time any model is loaded, Django iterates over every model in INSTALLED_APPS and creates the backward relationships in memory as needed. Essentially, one of the functions of INSTALLED_APPS is to tell Django the entire model domain.


Ref: https://docs.djangoproject.com/en/dev/topics/db/queries/#following-relationships-backward

Friday, October 26, 2012

Two Foreign Keys to same model require related_name definition

When creating a new model that requires two foreign keys to refer to the same other model, django requires you to define the related_name attribute for the ForeignKey field.

The related_name attribute specifies the name of the reverse relation from the User model back to your model.
If you don't specify a related_name, Django automatically creates one using the name of your model and the suffix _set.

Thus unless you specify a unique related_name for at least one of your FKs, your models won't validate throwing an error:
Accessor for field 'primary' clashes with related field '<FKModelname>.<modelname_set>'

 Ref: http://stackoverflow.com/questions/2642613/what-is-the-related-name-mean-in-django/2642645#2642645

Friday, October 19, 2012

Display indentation lines/code blocks in PyDev - Eclipse using EditBox

If you've used editors like Notepad++ for programming you must've noticed how helpful it is to have the indentation lines displayed. Especially when programming in Python where we have indentation-based scopes.

I missed this in Eclipse and this often resulted in silly bugs which were the result of incorrectly indented lines of code resulting in an incorrect scope.

This is where EditBox comes in.

It is a plugin for Eclipse which encapsulates all code within the same indentation-level/scope into a single colored block. If you do not want the colors, it also provides simple indentation lines (like Notepadd++).

Installation

Use update site: http://editbox.sourceforge.net/updates or install manually:
  1. Download plugin jar file into Eclipse dropins or plugins directory.
  2. Start Eclipse.
  3. To enable Editbox press the button located on the toolbar. Note: Depending on your configuration it might be required to turn off current line highlighting (see Window->Preferences->EditBox).

Thursday, October 18, 2012

Useful Git branch graphs

There's always the easy way of doing Git branch graphs in terminal by entering:
$ git log --graph --oneline --all

It's pretty useful, however here are a couple of excellent commands I came across on SO (thanks to user: Slipp Douglas ) which I use all the time and I thought I'd share them here..

log --graph --all --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(bold white)— %an%C(reset)%C(bold yellow)%d%C(reset)' --abbrev-commit --date=relative




log --graph --all --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n'' %C(white)%s%C(reset) %C(bold white)— %an%C(reset)' --abbrev-commit



The first one is what I use very often since it shows the author and more importantly how long ago a commit was made. So I can very quickly just scan the "how long ago" section to identify any recently made changes without having to read dates and make sense of them

However, in some cases when you do need the dates, the second command specifies the entire date and time too but it spans on 2 lines instead of just 1. Very useful nevertheless.

Ref: http://stackoverflow.com/a/9074343/1415352

Friday, October 12, 2012

Where is the apache error log!? [Ubuntu]

Q. Where is the apache error log on ubuntu?
A. Here: /var/log/apache2/error.log

Run tail -f /var/log/apache2/error.log to view the last few lines of the log, which are the most recent errors.

Thursday, October 11, 2012

Create terminal shortcut commands

If you often find yourself having to type long commands in the terminal, this tip could be very useful.

For example, to activate my virtualenv I usually have to run something like
$ source ~/.venvburrito/startup.sh
I've substituted that for just
$ activenv

To do this, all you have to do is edit your bashrc file
$ sudo nano ~/.bashrc

Scroll to the bottom and add your alias by adding a line like so..
alias activenv="source ~/.venvburrito/startup.sh"

In this manner you can add multiple aliases. Just make sure to name them wisely and not cause conflicts.
Save and quit (Cntrl+X > Y > Enter) and you're done!

Restart terminal and you should have your new command available to use!

Ref: http://lifehacker.com/270799/create-terminal-shortcuts

Wednesday, October 10, 2012

Display active git branch in bash prompt

For linux..

$ sudo nano ~/.bashrc

Add the following line to the bottom..

PS1="\u@\h:\w\$(git branch 2>/dev/null | grep -e '\* ' | sed 's/^..\(.*\)/{\1}/') \$ "

Save and quit. Restart terminal. cd into your git repo and you should see you're current branch.

Ref: http://gregk.me/2011/display-active-git-branch-in-bash-prompt/

How to fix django runserver 'port already in use' error

This usually happens if you  $ python manage.py runserver 8000 and then don't stop the server by hitting Cntrl+C but hit something like Cntrl+Z instead.

To stop the server on port 8000 so that you can run another instance again you can do the following..

Run the below command to list all your processes with 'manage' in their name.
$ ps aux | grep -i manage

Note the process_id (pid)  for our "manage.py runserver" process which should be the second column from the left.

And then simply run
$ kill -9 <pid>

e.g. $ kill -9 30144

Ref: http://stackoverflow.com/a/5788609/1415352

Tuesday, October 9, 2012

How to block IP addresses on your Linux server

This link is a very comprehensive and quick guide on how to block IP addresses on your Linux server using Iptables.

Gist:
View all blocked IPs

$ sudo /sbin/iptables -L INPUT -v -n


Block an IP

$ sudo /sbin/iptables -I INPUT -s 11.22.33.44 -j DROP


Unblock an IP
$ iptables -D INPUT -s 11.22.33.44 -j DROP

Create aliases for hosts to quickly SSH in

The folks at  howtogeek.com have a handy little tip on How to create aliases for your hosts to enable you to quickly ssh into them!

The gist is to turn
$ ssh -i yourkey.pem username@123.456.789.012
into
$ ssh dev-server

To do so, edit ~/.ssh/config to look like..

Host dev-server
        User username
        HostName 123.456.789.012
        IdentityFile /path/to/yourkey.pem
        ServerAliveInterval 120
        ServerAliveCountMax 30


You can save multiple hosts like this.

Monday, October 1, 2012

git: 'svn' is not a command - Fix!

If while attempting to do a 'git svn clone...' you get the error
git: 'svn' clone is not a command

.. you simply need to install the git-svn package:
sudo apt-get install git-svn

Popular Posts