简体   繁体   English

如何在数据库中没有ForeignKey的情况下执行ManyToOne?

[英]How can I perform ManyToOne without ForeignKey in db?

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

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). 但是问题出在我的设计理念上,我不允许我的数据库(MySQL)中存在ForeignKey(硬外键)。

Is there any method to perform that in Django2.x? 在Django2.x中有执行此操作的任何方法吗?

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

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. 当我发布此问题时,我知道ForeignKey的属性(db_constraint),但是我无法通过Django自动生成的SQL。 It tells me can not find a field. 它告诉我找不到字段。

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. 只需通过manage.py将模型迁移到物理数据库,所有工作即可。您可以从Django提供的ForeignKey中获得所有收益,并且数据库中没有任何物理ForeignKey。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM