Brant Harris wrote:
> > How would this model be implemented in Django? The API provides a
> > special case where a recursive relationship back to the current
entity
> > is denoted by ForeignKey('self'), but for the more general case of
a
> > circular relationship I can't see a way to avoid the NameError.
> >
> > I know in SqlObject the class's name can be used instead of a
> > reference to the class, providing late binding to enable this kind
of
> > model.
>
> There is no current way of doing this.  Perhaps some of the main
> developers for Django could extrapolate further on this, but I
believe
> the idea is partially to restrict such "circular" relationships as
> they are considered poor database/model design.


Are these still considered poor database/model design?

Here's my situation:
My 'Person' objects have zero or more 'Locations'.
Locations have 'created_by' and 'modified_by' references to the
'Person' table.

The following abreviated model definitions cause 'django-admin.py
install' to fail:

class Location(meta.Model):
    address = meta.CharField(maxlength=255)
    # ...
    created_by = meta.ForeignKey(Person)
    mod_by = meta.ForeignKey(Person)


class Person(meta.Model):
    primary_address = \
        meta.ForeignKey(Location)
    fn = meta.CharField('first name', maxlength=255)
    ln = meta.CharField('last name', maxlength=255)
    # ...
    created_by = meta.ForeignKey('self',
                                 related_name='creator')
    mod_by = meta.ForeignKey('self',
                             related_name='modifier')


Running these through django-admin.py install results in:
Traceback (most recent call last):
...
line 21, in Location
    created_by = meta.ForeignKey(Person)
NameError: name 'Person' is not defined

I didn't find any other references to this issue in the docs or the
list archives.  Has anyone figured out a way to make this work or
perhaps an elegant and functional work-around?

Thank you

Eric.

Reply via email to