On Wed, Aug 19, 2009 at 12:56 PM, Idan Gazit <i...@pixane.net> wrote:

>
> Hey all,
>
> I'd like to add some custom exception to a model of mine,
> Foo.FrobNotAllowed, along the lines of ModelName.DoesNotExist.
>
> From looking at models/base.py, it looks like the pattern is to
> override __new__() and use add_to_class. Something like:
>
> def __new__(cls, name, bases, attrs):
>    new_class = super(MyModel, cls).__new__(cls, name, bases, attrs)
>    abstract = getattr(attr_meta, 'abstract', False)
>    if not abstract:
>        new_class.add_to_class('FrobNotAllowed', MyExceptionClass)
>    return new_class
>
> Is this the pattern I should be emulating for the kind of thing I'm
> seeking?


All you really need is:

class DoesNotExist(Exception):
    pass

I generally create a Utils app in my project with a modelUtils.py where I
put things like that. Then you can just import the exception and use it in
any of your models.py files.

--~--~---------~--~----~------------~-------~--~----~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to