0

I have stumbled upon a problem, which I can not for the life of me seem to figure out. I even talked to a professor in data handling, without getting closer to a solution. So now I'm reaching out to you guys, hoping to get some suggestions on what to do.

In short, I am trying to create a system holding information about cars, where cars can get parts installed.

Here is the specification for the database:

A car has exactly one owner. An owner can have many cars.

The owner shall be able to groups its cars, so that he can easily add the same part to more than one vehicle at once. The group should then have a name.

A vehicle can have speakers, seats, wheels, all with different sets of attributes. Note that the car may have zero of these parts. It may also have more than one of a part.

The speakers must contain info about prize and manufacturer. The seats must contain info about height, width and length. The wheels must contain info about diameter and manufacturer.

It must also be possible to priorities which part should be added first.

The way I have solved this, is by letting a car belong to a group, regardless of whether it should belong to a group or not. If it does not belong to a group, it will be the only car in that group.

I got the requirement about prioritizing part installation today, so this is not implemented in my solution bellow.

My models look like this:

class CarGroup(models.Model):
    name = models.CharField(max_length=30)
    owner = models.ForeignKey(owners.models.Owner) #Another model


class Car(models.Model):
    name = models.CharField(max_length=100, blank=True, null=True)
    group = models.ForeignKey(CarGroup)

# Abstract Part Type
class PartType(models.Model):
    usedBy = models.ForeignKey(CarGroup)

    class Meta:
        abstract = True

class Speaker(PartType):
    prize = models.DecimalField()
    manufacturer = models.CharField(max_length=100)

class Seat(PartType):
    height = models.DecimalField()
    width = models.DecimalField()
    length = models.DecimalField()

class Wheel(PartType):
    diameter = models.DecimalField()
    manufacturer = models.CharField(max_length=100)

The problem I have with this design, is that i don't know how to get the data out of the database. If I have a car, and want to show all the parts that belong to this car, I must now search through every Part-table (Seat, Wheel, Speaker), to see if any of the objects belongs to the car.

That means I have to do the following:

speakers = Speaker.objects.filter(group=CarGroup.id)
seats = Seat.objects.filter(group=CarGroup.id)
wheels = Whell.objects.filter(group=CarGroup.id)

And then check if these query sets contain any data.

I refuse to believe that this is my best option. Do you guys have any suggestions?

andrroy
  • 128
  • 1
  • 6
  • Have you tried: `CarGroup.objects.select_related().get(pk=whatever)` ? – Brandon Taylor Dec 24 '14 at 15:02
  • That only gives me the name of the CarGroup – andrroy Dec 24 '14 at 15:47
  • Setting a variable, `car_group` using the statement above, you should be able to access [related_model]_set.all: `car_group.speaker_set.all()`, etc. – Brandon Taylor Dec 24 '14 at 15:49
  • take a look at this method if you want to go a little meta http://stackoverflow.com/q/2233883/2535531 – DRC Dec 24 '14 at 15:54
  • @DRC Wow! That works really great. The only problem is that it also returns all Car-objects. Is there a way to exclude Car for that search, so to reduce database access? – andrroy Dec 25 '14 at 19:46

0 Answers0