Django中的多态 [英] Polymorphism in Django

查看:126
本文介绍了Django中的多态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下型号。如何从Entity表中访问继承表(Team and Athete)的 unicode ?如果运动员,如果Team和'firstname'和'lastname',我正在尝试显示所有显示name的实体的列表。

  class Entity(models.Model):
entity_type_list =(('T','Team'),('A','Athlete'))
type = models.CharField(max_length = 2,choices = entity_type_list,default ='P')
pictureurl = models.URLField('Picture Url',verify_exists = False,max_length = 255,null = True,blank = True)


class Team(Entity):

name = models.CharField(max_length = 100)

def __unicode __(self):
return self $ name

class运动员(实体):

firstname = models.CharField(max_length = 100)
lastname = models.CharField(max_length = 100)

def __unicode __(self):
return'%s%s'%(self.firstname,self.lastname)


解决方案

这个

问题 Entity.objects.all()
打印unicode(实体)#实体Entity类unicode调用Entity类unicode调用Entity类unicode(实体)#

这不是你想要的。 

解决方案



在上面链接的答案中使用 InheritanceCastModel mixin作为实体的基类。然后,您可以从Entity实例转换为实际的派生类实例。当您想在父类(实体)上使用查询集,但是访问派生类实例时,这是特别方便的。

 类实体(InheritanceCastModel):
#您的模型定义。您可以摆脱entity_type_list和类型,因为InheritanceCastModel提供的
#real_type提供此信息

class运动员(实体):
#不变

class Team(Entity):
#不变

在Entity.objects.all()中的实体:
actual_entity = entity.cast()
print unicode (actual_entity)#实体是aa团队或运动员


I have the following models. How do I get access to the unicode of the inheriting tables (Team and Athete) from the Entity table? I'm trying to display a list of all the Entities that displays 'name' if Team and 'firstname' and 'lastname' if Athlete.

class Entity(models.Model):
  entity_type_list = (('T', 'Team'), ('A', 'Athlete'))
  type = models.CharField(max_length=2, choices=entity_type_list,default='P') 
  pictureurl = models.URLField('Picture Url', verify_exists=False, max_length=255, null=True, blank=True)


class Team(Entity):

  name = models.CharField(max_length=100)

  def __unicode__(self):
    return self.name

class Athlete(Entity):

  firstname = models.CharField(max_length=100)
  lastname = models.CharField(max_length=100)

  def __unicode__(self):
    return '%s %s' % (self.firstname, self.lastname)

解决方案

This answer from Carl Meyer to the question mentioned earlier by Paul McMillan might be what your looking for. A subtlety to this problem not captured in some of the answers is how to get at derived class instances from a QuerySet on Entity.

The Problem

for entity in Entity.objects.all()
  print unicode(entity) # Calls the Entity class unicode, which is not what you want.

A Solution

Use the InheritanceCastModel mixin in the answer linked above as a base class for Entity. You can then cast from Entity instances to the actual derived class instances. This is particularly handy when you want to use querysets on your parent class (Entity) but access the derived class instances.

class Entity(InheritanceCastModel):
  # your model definition. You can get rid of the entity_type_list and type, as the
  # real_type provided by InheritanceCastModel provides this info

class Athlete(Entity):
  # unchanged

class Team(Entity):
  # unchanged

for entity in Entity.objects.all():
  actual_entity = entity.cast()
  print unicode(actual_entity) # actual entity is a a Team or Athlete

这篇关于Django中的多态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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