Stephen Koplin
Stephen Koplin

Reputation: 87

Django - Get name of model object by iterating through class

I'm new to Python (and pretty inexperienced at programming in general), but I have an issue I can't for the life of me figure out.

I'm trying to pre-populate a field in my database with the name of an instance from another model/class in my database. The model with the field I want to pre-populate is an "instance" of the model instance from which I'm trying to grab the name, and has a foreign key to that instance.

Goal: 1) User selects the parent of the object by assigning the foreign key to the parent 2) A function grabs the name of the parent instance matching the foreign key the user selected. 3) the result from that function is used as the default value for the field.

Here's the code:

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})'

The problem area is def set_injury_name and Injury_Instance.name

Thanks!!!

Edit:

I tried the following code, but I'm getting the error 'NameError: name 'self' is not defined'

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})'

Upvotes: 0

Views: 214

Answers (1)

Sergey Pugach
Sergey Pugach

Reputation: 5669

You don't need to store the name of your foreign key field as you always can access it with:

self.injury_model.name

If you need to get access it just by name you can write a property in Injury_Instance model.

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

Upvotes: 1

Related Questions