多对多关系.以def __str__方法返回字段 [英] ManyToMany Relationships. Returning fields in def __str__ method
问题描述
我有两个模型:
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 __
方法:
And Movement
model. Please review the __str__
method of this model at the end:
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
的模型,在该模型中,我希望可以选择多个受影响的段和多个动作,例如:
I have another model named RehabilitationSession
in which I want allow the possibility of choose multiple affected_segments and multiple movements such as follow:
RehabilitationSession(models.Model)类:
class 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 Admin中,当我进入 RehabilitationSession
模型时,我在 motion
字段中看到以下内容:
In my Django Admin when I go to the RehabilitationSession
model I see the following in the movement
field:
我了解到,在 ManyToMany
关系中,使用两个表的ID字段创建了一个中间表,这两个表以这种方式形成 m2m
关系:
I understand that in the ManyToMany
relationships, a intermediate table is created with the ID's fields of the two tables which make the m2m
relationship of this way:
我的问题是:
如何在我的 Movement
模型中做到这一点,我可以访问要在 __ str __
方法中调用的 corporal_segment_associated
字段?
How to can I make that in my Movement
model I can access to the corporal_segment_associated
field that I want call in the __str__
method?
任何方向都非常有用:)
Any orientation is very useful :)
推荐答案
问题是 self.corporal_segment_associated
不是相关项目的列表,它是 ManyRelatedManager
.调用 str(self.corporal_segment_associated)
时,它将返回您在屏幕快照中看到的 AffectedSegment.None
字符串.
The problem is that self.corporal_segment_associated
is not a list of the related items, it is a ManyRelatedManager
. When you call str(self.corporal_segment_associated)
, it returns the AffectedSegment.None
string which you see in your screenshots.
要获取相关的页面,可以执行 self.corporal_segment_associated.all()
.这将返回相关对象的查询集.然后,您必须先将此查询集转换为字符串,然后才能在 __ str __
方法中使用它.例如,您可以这样做:
To fetch the related segements, you can do self.corporal_segment_associated.all()
. This will return a queryset of the related objects. You then have to convert this queryset to a string before you use it in the __str__
method. For example, you could do:
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查询.您可能会发现这会导致性能下降.
It might not be a good idea to access self.corporal_segment_associated.all()
in the __str__
method. It means that Django will do extra SQL queries for every item in the drop down list. You might find that this causes poor performance.
这篇关于多对多关系.以def __str__方法返回字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!