I use such way to get around that. Don't know is there any better ways to do it though.

def is_mutual_friend(friend):
  """
  Test whether myself and friend is mutual friend, which means we both
  are in each other's friend list
  """
  # The next line can't be moved to the top of the file
  from django.models import get_module
  mod = get_module('portal', 'friends')
  try:
    f = mod.get_object(myself__id__exact=friend.get_friend().id,
          friend__id__exact=friend.get_myself().id)
  except (KeyError, mod.FriendDoesNotExist):
    return False
  else:
    return True

- Cheng Zhang

On Jan 10, 2006, at 3:58 PM, Greg wrote:


When writing a method for one of my model classes, I find that I want
to access a function from another module. The reference documentation
(http://www.djangoproject.com/documentation/model_api/#model-methods)
is plenty clear on the fact that you can't just import a module at the
top of the model-definition file and then use it from within a method,
but what's the best workaround? I ended up passing it in as a default
argument:

  from django.core import meta
  import my_module
  ...
  class Poll(meta.Model):
      ...
      def my_method(self, my_module=my_module):
          return my_module.foo(self.question)

but that's not beautiful of course. And I could put the import inside
my method, but that's not ideal either. Any thoughts? Is this a
restriction that will be lifted by the magic removal branch? (And I
don't mean to sound whiny but when's the magic removal work planned to
be merged to the trunk? I can't wait!)


Reply via email to