Hey,

I might have some ideas now. I have not understood the problem earlier.

On Wed, 2008-05-07 at 12:52 +0200, Christopher Mutel wrote:
> Hello all-
> 
> I have tried a couple of tips found on the list to solve my problem
> with circular model imports, but with no lucks. Here is my basic
> schema:
> 
> #foo/models.py
> 
> from bar.models import Formula
> 
> class Variable(models.Model):
>   amount = models.FloatField(null=True)
>   is_formula = models.BooleanField(default=False)
>   formula = models.OneToOneField(Formula, null=True,
> related_name="variable_formula")
> 
>   def get_amount(self):
>     if self.is_formula:
>       return self.formula.evaluate()
>     else:
>       return self.amount
> 
> #bar/models.py
> 
> from foo.models import Variable
> 
> class Formula(models.Model):
>   formula = models.TextField() # Where formula is a text string like
> 'var:1337*var:42', and the numbers are id numbers.
> 
>   def calculate(self):
>     # re stuff
>     for each_found_variable in groups(): # Simplified to show process
> - this obviously doesn't work.
>       raw_string.append(Variable.objects.get(id=this_id).get_amount())
>     return eval(raw_string, stuff_to_make_eval_safe)
> 
> In this case, the circular reference occurs only in a method of the
> class - I don't know if this makes a difference. How can I structure
> my import statements so that this is possible? I should note that
> there are more than one variable, so that combining everything into
> one models file is not really possible (and doesn't really address the
> chicken/egg problem either).
> 
> I thought I saw a recent svn commit that addressed this problem, but
> can't find it now.
> 
> Any help would be greatly appreciated?
> 

The commit you refer to might be something from the now merged qs-rf
branch, where you can specify models from other django applications with
a new syntax like ForeignKey('app.Variable')

I can think of two methods of loading Variable inside your calculate
function:

- Write the import statement in the function instead of writing it at
the top of the module.
- Use django.db.models.loading.get_model('app', 'Variable')

I hope that helps,

Matthias


-- 
http://spinlock.ch/blog/

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to