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).

It occurs at import time. When you do from bar.models import Formula 
bar.models is interpreted which then does from foo.models import 
Variable and you're stuck.

You need to import the model files as modules. That is, you need to do 
`import foo.models` in bar/models.py and `import bar.models` in 
foo/models.py.

Then you should be able to do `foo.models.Variable.objects...`.

HTH,
Christian

-- 
Christian Joergensen
http://www.technobabble.dk

--~--~---------~--~----~------------~-------~--~----~
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