Django: “Soft” ForeignField without database integrity checks

前端 未结 6 1319
南旧
南旧 2021-02-05 17:05

I have a Django project that has multiple django \"apps\". One of them has models to represent data coming from an external source (I do not control this data).

I want m

6条回答
  •  再見小時候
    2021-02-05 17:34

    You could try using an unmanaged model:

    from django.db import models
    
    
    class ReferencedModel(models.Model):
        pass
    
    
    class ManagedModel(models.Model):
        my_fake_fk = models.IntegerField(
            db_column='referenced_model_id'
        )
    
    
    class UnmanagedModel(models.Model):
        my_fake_fk = models.ForeignKey(
            ReferencedModel, 
            db_column='referenced_model_id'
        )
    
        class Meta:
            managed = False
            db_table = ManagedModel._meta.db_table
    

    Specifying managed=False in a Model Meta class will not create a db table for it. However, it will behave exactly like other models.

提交回复
热议问题