brno792
brno792

Reputation: 6799

Django models. Associating tuples in multiple model classes and admin

A newbie Django question about associating data in models.

I have three tables, CarMake, CarModel and Driver.

CarMake has several car brands in it.

CarModel has several models of each of those brands.

Here is some basic code I was considering:

from django.db import models

class CarMake(models.Model):
    MAKE = (
        ('Ford'),
        (''Toyota'),
        ('Volkswagon'),
    )

    car_make = models.CharField(choice=MAKE)

class CarModel(models.Model):
    MODEL = (
        ('Ford'
            ('Focus'),
            ('Explorer'),
            ('F150'),
        )
        ('Toyota'
            ('Avalon'),
            ('Highlander'),
            ('Tundra'),
        )
        ('Volkswagon'
            ('Golf'),
            ('Jetta'),
            ('Passat'),
        )
    )

    car_model = models.CharField(choice=MODEL)

class Driver(models.Model):
    driver_name = models.CharField(max_length=120)
    driver_car = '''choose make and model'''

Upvotes: 2

Views: 965

Answers (2)

davierc
davierc

Reputation: 276

From what you have described, it sounds like you want a one to many relationship between CarMake and CarModel. e.g. One CarMake can have multiple CarModels, but one CarModel can not have multiple CarMakes. Here is some code to get you started:

# In app/models.py
class CarMake(models.Model):
    name = models.CarField(max_length=30)


class Car(models.Model):
    model = models.CarField(max_length=30)
    make = models.ForeignKey(CarMake)


class Driver(models.Model):
    driver_name = models.CharField(max_length=120)
    car = models.ForeignKey(Car)

Creating Make Models

make = CarMake(name='Toyota')
make.save()

car = Car(make=make, model='Avalon')
car.save()

Registering in Admin

# in app/admin.py
from django.contrib import admin
from app.models import CarMake, CarModel, Driver

admin.site.register(CarMake)
admin.site.register(CarModel)
admin.site.register(Driver)

Upvotes: 0

ryaanwells
ryaanwells

Reputation: 280

Look into the Django Models Documentation to find out how to create your models.py. I see the exact idea of what you're getting at in this file but you're combining two things.

models.py should contain the entities and how they are connected not the entities and the possible instances of these entities. models.py defines the database structure not its contents - this is important to note.

As a few pointers:

  • CarModel should have a ForeignKey of the CarMake table. This will define the relationship between a car and who makes it. Then you will need to define another field to hold the model of that car. I'm not sure how to enforce only selecting cars from a certain manufacturer (but that's a question in it's own right) - maybe this should be in the front-end logic?
  • Driver should also have a ForeignKey of CarModel to define what car this driver drives. note how many cars can one driver drive? You'll need to decide if they can only drive one or if they can drive many.

Have a good read at the documentation before going on, it'll help you a great deal more than anything else and is always the best place to start.

Upvotes: 1

Related Questions