如何在数据库中没有ForeignKey的情况下执行ManyToOne? [英] How can I perform ManyToOne without ForeignKey in db?

查看:125
本文介绍了如何在数据库中没有ForeignKey的情况下执行ManyToOne?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

eb_config_object = models.ForeignKey(EbConfig, on_delete=models.CASCADE)

我要序列化一个对象,其中包括另一个对象。但是问题出在我的设计概念中,我不允许我的数据库(MySQL)中存在ForeignKey(硬外键)。

I want to serialize an object, which includes another object. But the problem is in my design concept I can not allow ForeignKey(Hard foreign key) exist in my database(MySQL).

在Django2中是否有任何方法可以执行该操作.x?

Is there any method to perform that in Django2.x?

推荐答案

eb_config_object = models.ForeignKey(EbConfig, on_delete=models.CASCADE, db_constraint=False)

当我发布此问题时,我知道ForeignKey的属性(db_constraint) 。但是我无法通过Django自动生成的SQL。它告诉我找不到字段。

When I post this question, I had known the attributes(db_constraint) of ForeignKey.But I failed to get through the SQL automatic generated by Django. It tells me can not find a field.

只需通过manage.py将模型迁移到物理数据库,所有工作即可。您可以从ForeignKey获得所有好处由Django提供,并且您的数据库中没有任何物理ForeignKey。

Just migrate your models to physical database by manage.py, and all works.You can get all the benifits from ForeignKey which is given by Django, and there is no any physical ForeignKey in your database.

这篇关于如何在数据库中没有ForeignKey的情况下执行ManyToOne?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆