使用外键从父模型到一个子模型是否可以获取两个字段?
考虑以下模型,
class country_city(models.model):
country_name = models.charfield(max_length = 200)
code> city> city> city_name = models.charfield(max_length = 200)
class register_user(models.model):
country = models.ForeignKey(Country, on_delete=models.CASCADE,related_name='country', null=True)
city = models.ForeignKey(Country_City,on_delete=models.CASCADE,related_name='city',null=True)
是正确的使用方法吗?
我想从父型到子模型中获得两个字段
consider the below models,
class Country_City(models.Model):
country_name = models.CharField(max_length=200)
city_name = models.CharField(max_length=200)
class Register_user(models.Model):
country = models.ForeignKey(Country, on_delete=models.CASCADE,related_name='country', null=True)
city = models.ForeignKey(Country_City,on_delete=models.CASCADE,related_name='city',null=True)
is it a right way to use?
I want to get two fields from parent model to child model
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这很简单,您可以访问从父表到子表以及从子表到父表的所有字段。 Django 还为您提供了外键功能和反向关系。
您可以创建这样的模型,如果您想访问从子表到父表的任何字段,那么您可以使用外键访问父表中的任何字段,也可以反向访问。
That's simple you can access all fields from parent table to the child table and child to parent. Django gives you foreign key feature and reverse relationship as well.
You can make models like this and if you want to access any fields from child to parent then you can use foreign key to access any fields from parent table and reverse as well.