title: Django - passing extra arguments into default callable function
body: <h1>Original Title: cannot get <strong>import</strong> to work properly</h1>
  <p>I am trying to generate a function which will create a random alphanumeric number and make it as default value of a model field in django.</p>
  <p>So for a single model I did like this:</p>
  <pre><code># utils.py
  def generate_random_unique_code_for_model():
      from .models import mymodel 
      while 1:
          code = random_code()  #my custom function to generate random alphanumeric
          try:
              mymodel.objects.get(myfield=code)
          except mymodel.DoesNotExist:
              return code
  
   #models.py
   class mymodel(models.Model):
       #other fields
       myfield = models.CharField(default=generate_random_unique_code_for_model)
  </code></pre>
  <p>This code works fine, but now I have to provide similar function for another model, so to follow the DRY principle I am trying to make the model , fieldnames dynamic. So basically I am trying to accomplish <code>from some_app.models import some_model</code> inside my <code>generate_random_unique_code_for_model</code> function .</p>
  <pre><code>def get_model(location, model_name):
      try:
          module = __import__('.'.join(location), globals(), locals(), [model_name], -1)
          model_instance = getattr(module, model_name)
      except:
          raise ImportError(_('Could not import %(model_name)s from %(location)s') % {'model_name': model_name,
                                                                                      'location': '.'.join(location)})
      return model_instance
  
  
  def generate_random_unique_code_for_model(location, model_name, field_name):
      model_object = get_model(location, model_name)
      kwargs = {field_name: ''}
      while 1:
          code = random_code()
          kwargs[field_name] = code
          try:
              model_object.objects.get(**kwargs)
          except model_object.DoesNotExist:
              return code
  #models.py
  class mymodel_name(models.Model):
      #other fields
      myfield_name = models.CharField(default=generate_random_unique_code_for_model(['myapp_name', 'mymodel_name'], 'myfield_name'))      
  </code></pre>
  <p>While debuggin, when I do <code>dir(module)</code> while debugging I don't see <code>mymodel_name</code> in the list. Any workarounds please?</p>