1

I am facing a problem designing a database with Django framework that i can't seem to solve. Say i have the following models:

class Report(models.Models):
    equipment = models.ForeignKey(Equipament)
    category = models.ForeignKey(Category)
    date = models.DateField()
    user = models.CharField(max_length=100)

class TestA(models.Model):
    report = models.ForeignKey(Report)
    lasers = models.FloatField()
    table = models.FloatField()
    dose = models.FloatField()
    pass_fail = models.NullBooleanField()

class TestB(models.Model):
    report = models.ForeignKey(Report)
    ctdi = models.FloatField()
    pass_fail = models.NullBooleanField()

class TestC(models.Model):
    report = models.ForeignKey(Report)
    pass_fail = models.NullBooleanField()

My question is: Is it possible to get all objects from Tests B, C and D that share the same oject from Report? I do not wish to query each model (TestB, TestC or TestD) individually because in reality i have many more Test models.
Any help is much apreciated.

user2466766
  • 195
  • 1
  • 11
  • Are you saying you have one ModelA object, and you want to get the related ModelB, C, and Ds? This seems like it may be related to your question as well http://stackoverflow.com/a/2315053/4974980 – Jens Astrup Oct 02 '16 at 21:28
  • @JensAstrup Thanks. That is exactly what i want to do. I will give it a look. – user2466766 Oct 02 '16 at 21:35
  • @JensAstrup Thanks for the tip. Although it was a similar problem i wasn't able to solve it. It seems that "get_all_related_objects()" method used as a solution has been deprecated. It seems like a "doable" thing. I am seeking to query a ModelA object and get all the related ones from the other models... – user2466766 Oct 02 '16 at 22:29
  • What do all these related models look like (in terms of fields contained). And rather than using ABC, can you post a good sample. Thanks – e4c5 Oct 03 '16 at 03:42
  • @e4c5 Updated my post. I am performing tests to equipment. Each test has different values to be saved so i created different models for each one of them. I though to relate all of the tests by giving them a foreign key to the same report object so i could actually present a report with all the test. Thought it would be easy but can't seem to work this out. – user2466766 Oct 03 '16 at 09:05

1 Answers1

1

Why do you have three different models that are essentially the same thing? Try this:

class Report(models.Models):
    equipment = models.ForeignKey(Equipment)
    category = models.ForeignKey(Category)
    date = models.DateField()
    user = models.CharField(max_length=100)

class Result(model.Model):
    report = models.ForeignKey(Report)
    status = models.BooleanField(default=False)
    lasers = models.FloatField(blank=True, null=True)
    table = models.FloatField(blank=True, null=True)
    ctdi = models.FloatField(blank=True, null=True)
    dose = models.FloatField(blank=True, null=True)
    title = models.CharField(max_length=200)

Just give each report a title, such as 'TestA' / 'TestB' instead of creating an entirely new model.

To get all results for a report:

r = Result.objects.filter(report=Report.objects.get(pk=1))
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • You mean give each result a title...I didin´t do it like this because each test to be performed to equipments is different. TestA doesn't have all those fields... – user2466766 Oct 03 '16 at 19:14
  • That's why all those fields are optional and default to `NULL`. So you only have to enter the fields that make sense for the kind of test you are doing. – Burhan Khalid Oct 04 '16 at 03:16
  • @BurhanKhalidi It does make things easier to relate. Is there a way to automatically load only some fields based on a FK choice? for example If i select category in the Report to load only some fields? Otherwise everything would be hardcoded in the html when i would render the form. – user2466766 Oct 04 '16 at 09:02
  • You can easily take care of this in the view. – Burhan Khalid Oct 04 '16 at 09:10
  • Thank you. I will give it a try although it doesn´t seem very realistic. – user2466766 Oct 04 '16 at 14:47