2016-09-27 68 views
1

返回场,我有两个型号:ManyToMany关系。在高清__str__方法

AffectedSegment模型

class AffectedSegment(models.Model): 

    SEGMENTO_ESCAPULA = 'Escápula' 
    SEGMENTO_HOMBRO = 'Hombro' 
    SEGMENTO_CODO = 'Codo' 
    SEGMENTO_ANTEBRAZO = 'Antebrazo' 
    SEGMENTO_CARPO_MUNECA = 'Carpo/Muñeca' 
    SEGMENTO_MANO = 'Mano' 
    SEGMENT_CHOICES = (
     (SEGMENTO_ESCAPULA, u'Escápula'), 
     (SEGMENTO_HOMBRO, u'Hombro'), 
     (SEGMENTO_CODO, u'Codo'), 
     (SEGMENTO_ANTEBRAZO, u'Antebrazo'), 
     (SEGMENTO_CARPO_MUNECA, u'Carpo/Muñeca'), 
     (SEGMENTO_MANO, u'Mano'), 
    ) 

    affected_segment = models.CharField(
     max_length=12, 
     choices=SEGMENT_CHOICES, 
     blank=False, 
     verbose_name='Segmento afectado', 
     help_text='Ingrese uno o mas segmentos corporal de miembros superiores' 
    ) 

    class Meta: 
     verbose_name_plural = 'Segmentos corporales' 

    def __str__(self): 
     return "%s" % self.affected_segment 

而且Movement模型。请在最后审查了该模型的__str__方法:

class Movement(models.Model): 
    type = models.CharField(
     max_length=255, 
     verbose_name='Tipo de movimiento' 
    ) 

    corporal_segment_associated = models.ManyToManyField(
     AffectedSegment, 
     blank=True, 
     verbose_name='Segmento corporal asociado') 

    class Meta: 
     verbose_name = 'Movimiento' 

    def __str__(self): 
     return "{},{}".format(self.type, self.corporal_segment_associated) 

我在,我想叫RehabilitationSession另一种模式允许选择多个affected_segments和多运动,如后续的可能性:

类RehabilitationSession( models.Model):

affected_segment = models.ManyToManyField(
    AffectedSegment, 
    verbose_name='Segmento afectado', 
    #related_name='affectedsegment' 
) 
movement = models.ManyToManyField(
    Movement, # Modelo encadenado 
    verbose_name='Movimiento', 
    #related_name = 'movement' 
) 

在我的Django管理,当我去到RehabilitationSession模式,我看到movement领域以下内容:

enter image description here

据我所知,在ManyToMany关系,一个中间表与两个表,这使得这种方式的m2m关系的ID的字段创建:

enter image description here

我的问题是:

如何在我的Movement模型中创建该模型我可以访问我想要的corporal_segment_associated字段请拨打__str__方法?

任何方向是非常有用的 :)

回答

1

的问题是,self.corporal_segment_associated不相关的项目列表,它是一个ManyRelatedManager。当您拨打str(self.corporal_segment_associated)时,会返回您在屏幕截图中看到的AffectedSegment.None字符串。

要获取相关的分段,您可以做self.corporal_segment_associated.all()。这将返回相关对象的查询集。然后在__str__方法中使用它之前,必须将此查询集转换为字符串。例如,你可以这样做:

class Movement(models.Model): 
    def __str__(self): 
     corporal_segment_associated = ", ".join(str(seg) for seg in self.corporal_segment_associated.all()) 
     return "{},{}".format(self.type, corporal_segment_associated) 

它可能不是在__str__方法来访问self.corporal_segment_associated.all()一个好主意。这意味着Django将为下拉列表中的每个项目执行额外的SQL查询。您可能会发现这会导致较差的性能。

+0

感谢您的回复。当您告诉我有关每个项目的额外SQL查询时,这是真的。在这种情况下,有一些方法可以衡量这种表现。 – bgarcial

+1

我没有考虑测量性能的具体工具。如果页面加载足够快,那么我不会担心它太多。如果速度慢,那么你可以使用'prefetch_related'来加速它(见[这个答案](http://stackoverflow.com/a/39698396/113962))。如果这还太慢,你需要考虑一种不同的方法。 django-debug-toolbar可以用来查看页面的SQL查询。 – Alasdair

+0

非常好,非常感谢@alasdair – bgarcial