Currently I have three models in Django that create a circular reference:
A User can live in a Location.
A Location must be part of a Property.
A Property must have an owner, which is a User.
The reason I want each User to specify a location is for people living in apartments. An apartment tenant would live in a numbered room, but a house tenant wouldn’t. But notice that location can also simply be just a property (ie a house tenant lives in a location which is just the property with an address; the property doesn’t have room numbers, floors, or buildings.).
Here is the (stripped down) code:
class User( models.Model ) :
TYPE_CHOICES = (
( 't', 'tenant' ),
( 'o', 'property owner' ),
( 'v', 'vendor' ),
( 'm', 'property manager' ),
)
user_type = models.CharField( max_length = 1, choices = TYPE_CHOICES, default = 't' )
first_name = models.CharField( max_length = 135 )
last_name = models.CharField( max_length = 135 )
location = models.ForeignKey( Location, null = True, blank = True )
class Property( models.Model ) :
name = models.CharField( max_length = 135 )
owner = models.ForeignKey( User )
address_line_one = models.CharField( max_length = 135 )
address_line_two = models.CharField( max_length = 135, blank = True )
city = models.CharField( max_length = 135 )
state = models.CharField( max_length = 135 )
zip_code = models.CharField( max_length = 135 )
class Location( models.Model ) :
room = models.CharField( max_length = 135, blank = True )
floor = models.CharField( max_length = 135, blank = True )
building = models.CharField( max_length = 135, blank = True )
prop = models.ForeignKey( Property )
Please let me know if you guys need more clarification or code. Thanks in advance!
Like the docs say, you can use a string to specify the app and model to use in a relation.