Yesterday, during a meeting with my Drupal friends, I showed them some Python magic including Fabric () and how it can be used to make your Drupal dev life easier. My friends been blown away ;) I promised to share my regular fabfile.py for a Drupal project, please see it below, just a note: this is really beginner's code and I'm not responsible for any damages or losses it can bring. Okay, here it is:

When you save your main object and want to attach / create / modify attached to it related models you need to remember the following:

1) Do this in post_save signal, because related objects need pk and you get pk in post_save (when you create a new object)

2) If you manipulate ForeignKey or OneToOneField in post_save(), no additional moves needed. Manipulate there, don't forget to save() objects after the manipulation. For example: main.related.save()

Surprise surprise! When you save a model via admin forms it's not an atomic transaction. The main object gets saved first (to make sure it has a PK), then the M2M is cleared and the new values set to whatever came out of the form. So if you are in the save() of the main object you are in a window of opportunity where the M2M hasn't been updated yet. In fact, if you try to do something to the M2M, the change will get wiped out by the clear().

There are situations, when you or other modules alter username via hook_username_alter() and this can change username dramatically: for example, you can alter username into a combination of First and Last Name, etc.

Django sitetree is a great app to manage your Django app menus. Sometimes, you'll have a page that you want to manually put under a sitetree, so how to do that? At first we need to patch a sitetreeapp.py like this:

To compare two times you need to do the following:

If you have a parent model that has OneToOne relation to some other model and you want this child model to be created upon parent model creation you need to install django-annoying package like this:

pip install django-annoying

enable annoying app and replace your models.OneToOneField with AutoOneToOneField like this:

Surprise, surprise, when you want to perform some actions on model delete, overriding delete() method may be not enough, because when you do bulk delete of models via admin interface it won't touch model's delete() method. The way out of it is using signals:

If you've missed DrupalCon Prague 2013 or, maybe, you want to relive once again this great party go and see all videos from the event on this page: all DrupalCon 2013 Prague videos. Or use the playlist embed below. There you'll find 4 days, 7 hours long video material (104 videos)!

Pages

Subscribe to TimOnWeb RSS