gpt4 book ai didi

python - 解决 SQL 中的三向循环引用 (Django)

转载 作者:行者123 更新时间:2023-11-28 22:03:16 26 4
gpt4 key购买 nike

目前我在 Django 中有三个创建循环引用的模型:

用户 可以住在位置Location 必须是 Property 的一部分。Property 必须有一个所有者,即 User

我希望每个 User 指定一个位置的原因是为了住在公寓里的人。公寓租户会住在有编号的房间里,但房屋租户不会。但请注意,位置也可以只是一个特性(即,房屋租户居住的位置只是具有地址的特性;该特性没有房间号、楼层或建筑物。)。

这是(精简的)代码:

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 )

如果你们需要更多说明或代码,请告诉我。提前致谢!

最佳答案

the docs比如,您可以使用字符串来指定要在关系中使用的应用程序和模型。

If you need to create a relationship on a model that has not yet been defined, you can use the name of the model, rather than the model object itself

关于python - 解决 SQL 中的三向循环引用 (Django),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9543953/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com