Django OneToOneField to multiple models

◇◆丶佛笑我妖孽 提交于 2019-12-07 10:20:31

问题


Suppose we have a base model:

class BaseModel(models.Model):
    pass

with some subclasses:

class Submodel1(BaseModel):
    some_field = models.TextField()

...

class Submodel9(BaseModel):
    another_field = models.TextField()

Each submodel is defined in its own Django app. New apps with new submodels can appear.

We also have another model, let's call it RelatedModel, which should have a one-to-one relation to BaseModel:

class RelatedModel(models.Model):
    the_thing = models.OneToOneField(BaseModel, null=True, blank=True)

Is it possible to do define such a relation if BaseModel.Meta.abstract == True? Or without defining BaseModel at all?

I have posted some solutions as answers below, but they seem a bit ugly to me.


回答1:


https://stackoverflow.com/a/23547494/517316

Instead of putting the relation to RelatedModel, it is possible to put it to Submodel1 .. Submodel9.

class Submodel1(models.Model):
    some_field = models.TextField()
    related_model = models.OneToOneField(RelatedModel, 
                                         null=True, blank=True, 
                                         related_name='the_thing')

...

class Submodel9(models.Model):
    another_field = models.TextField()
    related_model = models.OneToOneField(RelatedModel,
                                         null=True, blank=True,
                                         related_name='the_thing')

Or, if we make BaseModel abstract, we can define it right in BaseModel:

class BaseModel(models.Model)
    related_model = models.OneToOneField(RelatedModel, 
                                         null=True, blank=True,
                                         related_name='the_thing')

    class Meta:
        abstract = True

This would allow accessing SubmodelX from an instance of RelatedModel using a field named the_thing, just as in the multi-table inheritance example.




回答2:


It is possible to achieve with GenericForeignKeys:

class RelatedModel(models.Model):
    content_type_of_the_thing = models.ForeignKey(ContentType)
    id_of_the_thing = models.PositiveIntegerField()
    the_thing = GenericForeignKey('content_type_of_the_thing', 'id_of_the_thing')

    class Meta:
        unique_together   = ('content_type_of_the_thing', 'id_of_the_thing')

    # TODO: restrict `content_type_of_the_thing` by `Submodel1 .. Submodel9` somehow
    # Take into account that new submodels can appear


来源:https://stackoverflow.com/questions/23555478/django-onetoonefield-to-multiple-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!