Django-通过遍历类获取模型对象的名称

时间:2018-12-13 02:35:52

标签: python django oop

我是Python的新手(通常在编程方面没有经验),但是我有一个我无法解决的问题。

我正在尝试使用数据库中另一个模型/类的实例名称预填充数据库中的字段。我要预填充字段的模型是我要从中获取名称的模型实例的“实例”,并且具有该实例的外键。

目标:1)用户通过将外键分配给父对象来选择对象的父对象。2)函数获取与用户选择的外键匹配的父实例的名称。 3)该函数的结果用作该字段的默认值。

代码如下:

class Injury(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, help_text='Unique ID for this particular injury')
    name = models.CharField(max_length=200, help_text='Enter an injury or complication (e.g. respiratory failure)')
    description = models.TextField(max_length=1000, blank=True, help_text='Describe the injury')
    time_to_onset = models.PositiveIntegerField(blank=True, validators=[MaxValueValidator(10000)], help_text='Enter expected time from trigger until injury/complication onset')
    findings = models.TextField(max_length=1000, blank=True, help_text='Enter the signs and symptoms of the injury or complication')
    vitals_trends = models.TextField(max_length=1000, blank=True, help_text='Enter the vitals changes due to the injury')

    class Meta:
        ordering = ['name']

    def __str__ (self):
        return self.name

    def get_absolute_url(self):
        return reverse('Injury-detail', args=[str(self.id)])




class Injury_Instance(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, help_text='Unique ID for this particular injury')
    parent_case = models.ForeignKey(PFC_Case, on_delete=models.SET_NULL,null=True)
    injury_model = models.ForeignKey(Injury, on_delete=models.SET_NULL, null=True)

    def set_injury_name(self):
        for injuries in Injury.all()
            if injury_model == injuries
                break
        return Injury.name

    name = dislay_models.CharField(default=set_injury_name(self,Injury,injury_model), max_length=100)
    triggered_by = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, blank=True)

    def __str__ (self):
        return self.name

    def get_absolute_url(self):
        return f'{self.id} ({self.Injury.name})'

问题区域是定义set_injury_nameInjury_Instance.name

谢谢!

编辑:

我尝试了以下代码,但出现错误“ NameError:未定义名称'self'”

class Injury_Instance(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, help_text='Unique ID for this particular injury')
    parent_case = models.ForeignKey(PFC_Case, on_delete=models.SET_NULL,null=True)        
    injury_model = models.ForeignKey(Injury, on_delete=models.SET_NULL, null=True)

    def get_injury_name(self):
        return self.name

    injury_name=get_injury_name(self)

    name = models.CharField(default=injury_name, max_length=100)

    triggered_by = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, blank=True)

    def __str__ (self):
        return self.name
    def get_absolute_url(self):
        return f'{self.id} ({self.Injury.name})'

1 个答案:

答案 0 :(得分:1)

您无需存储外键字段的名称,因为您始终可以通过以下方式访问它:

self.injury_model.name

如果您只需要通过name进行访问,则可以在Injury_Instance模型中编写属性。

@property
def name(self):
    return self.injury_model.name