2
votes

I am new to Django and I am trying to implement this Relationship in Django

Person has a Car Car can be tested for problems A car should be tested for a select set of criterias

So I implement it as

class Person(model.Model):
    name = models.CharField(max_length=60)
    license = models.CharField(max_length=80)

class Car(models.Model):
    name = models.CharField()
    owner = models.ForeignKey('Person')
    isDiesel = models.BooleanField()

I am trying to import the fields of car into test. Is there anyway way to do it? I am trying to replicate this SQL statement

 SELECT test FROM table WHERE OWNER IS x (object instance) AND CAR IS isDiesel 

Thanks in advance.

1

1 Answers

2
votes
Car.objects.filter(isDiesel=True, owner=person_instance)

This will return an array of Car objects.

You should have a read at the Django docs on this subject; this framework has a really badass documentation.