Search code examples
pythondjangodjango-modelsmany-to-many

Return a specific field using ForeignKey


class Client(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    client_name = models.CharField('Nom et prénom', max_length=120)

    def __str__(self):
        return self.client_name

class OtherInfos(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    client_name = models.ForeignKey(Client, null=True, on_delete=models.CASCADE)
    client_detail = models.TextField('détails', blank=True)

    def __str__(self):
        return str(self.client_name)

class information(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    info_name = models.ForeignKey(patientlist, null=True, on_delete=models.CASCADE)
    info_data = models.TextField('More info', blank=True)
    info_evolution = models.ForeignKey(OtherInfos, null=True, on_delete=models.CASCADE)

    def __str__(self):
        return str(self.info_name)

Hello everyone,

I have these 3 tables :

a "Client" can have multiple "information" and "OtherInfos", and i would like to show all data in one html page, i managed to do 90% of it using Foreignkey where i can get "information" and "Client" data in the html page, but it didn't work with "OtherInfos", any suggestions please ?

thank you


Solution

  • Well, the problem was that the uuid primary key change from one table to another even though it's the same Client, so when i try to do queries, it finds none, so what i did is i used AutoSlugField as primary key in all models :

    slug = AutoSlugField(populate_from='NAME')
    

    since all tables have the same 'NAME', that way i didn't need Foreignkeys...