Saturday, May 28, 2011

Re: signals pre_save vs model's save()

You can set more than 1 [pre,post]_save signal per-model, no problem. I generally, and I'm not saying this is the django way, do it like this:
app_name/
     __init__.py
     modes.py
     views.py
     signals.py

Inside my signals.py I have all my signal declarations and connections.
And inside __init__.py I just do:

import app_name.signals

Rgds,
Marcos
    

On Sat, May 28, 2011 at 7:05 AM, Mateusz Harasymczuk <mateusz@harasymczuk.pl> wrote:
I am thinking about splitting my model's save() method over few signals.

For example, stripping spaces and making string capitalized.

I have even more sophisticated problems such as making an object field active set to False basing on various parameters from other fields, such as expiration date, or good or bad karma points.

What do you think, it is generally good idea, to keep models file clean out of heavily overloaded save() methods?

How about making more than one signal pre_save to the same model?


@receiver(pre_save, sender=X)
def strip_chars(sender, **kwargs):
    pass

@receiver(pre_save, sender=X)
def capitalize_name(sender, **kwargs):
    pass

@receiver(pre_save, sender=X)
def make_inactive(sender, **kwargs):
    pass

Will it work?

I want to put those in signals/X.py
where X is my model name

Where to import them? in my model file?
or it will happen "automagicly" like with admin.py file?
(I think that python's explicit rule forbids that way, therefore where to import those signals, avoiding recurring imports [signal.py import model.py and model imports signals])?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

Jamie Zawinski, in comp.emacs.xemacs

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment