Django中的通用一对一关系

时间:2011-10-20 14:18:48

标签: python django django-models django-admin one-to-one

我需要建立一对一的关系,这种关系也必须是通用的。也许你可以建议我一个更好的设计。到目前为止,我提出了以下模型

class Event(models.Model):
    # skip event related fields...
    content_type      = models.ForeignKey(ContentType)
    object_id         = models.PositiveIntegerField()
    content_object    = generic.GenericForeignKey('content_type', 'object_id')

    class Meta:
        unique_together   = ('content_type', 'object_id')

class Action1(models.Model):
    # skip action1 related fields...
    events = generic.GenericRelation(Event, content_type_field='content_type', object_id_field='object_id')

    @property
    def event(self):
        return self.events.get() # <<<<<< Is this reasonable?

class Action2(models.Model):...

在事件列表中的Django Admin中,我想收集所有操作,然后从那里我想去管理页面进行操作。是否可以避免在动作模型中创建event属性?有更好的解决方案吗?将字段events和属性event组合在一个定义中会很好。我正在使用的项目使用Django 1.1

2 个答案:

答案 0 :(得分:15)

我最近came across this problem。你所做的很好,但你可以通过创建一个透明地反转关系的mixin来进一步概括它:

class Event(models.Model):
    content_type      = models.ForeignKey(ContentType)
    object_id         = models.PositiveIntegerField()
    content_object    = generic.GenericForeignKey('content_type', 'object_id')

    class Meta:
        unique_together   = ('content_type', 'object_id')

class EventMixin(object):
     @property
     def get_event(self):
         ctype = ContentType.objects.get_for_model(self.__class__)
         try:
             event = Event.objects.get(content_type__pk = ctype.id, object_id=self.id)
         except:
            return None 
         return event

class Action1(EventMixin, models.Model):
    # Don't need to mess up the models fields (make sure the mixing it placed before models.Model)
    ...

action = Action1.object.get(id=1)
event = action.get_event

您可能也希望将缓存添加到反向关系

答案 1 :(得分:2)

如果对象不存在,请使用.get()返回加薪;如果对象不存在,请.first()返回None。

名称events_relation是区分eventevents的一种优雅方法。

class Event(models.Model):
    content_type      = models.ForeignKey(ContentType)
    object_id         = models.PositiveIntegerField()
    content_object    = generic.GenericForeignKey('content_type', 'object_id')

    class Meta:
        unique_together   = ('content_type', 'object_id') # Important

class Action1(models.Model):
    events_relation = generic.GenericRelation(Event)

    @property
    def event(self):
        # Return the object in exists
        # else None 
        return self.events_relation.first()