-1

Currently i work on an application to manage drivers for cars and events written in Django, Python.

I have the two Models:

class Vehicle(models.Model):
    name            = models.CharField(max_length=100)
    max_capacity    = models.IntegerField(default=2)
    available       = models.BooleanField()

class Event(models.Model):
    start_timestamp = models.DateTimeField()
    end_timestamp   = models.DateTimeField()
    description     = models.TextField()
    location        = LatLonField()

I've a pool of 10 different vehicles saved.
There are 18 different Events.

Now i want to add multiple Vehicles to an event without to generate new ones for every event, and specify a number of people which will be using this car for this event like:

Event 0815:
    - FirstCar,  loaded with 5/13 People
    - SecondCar, loaded with 2/2 People

(The max capacity of a car is given in it's Model)

How is this with the structure of djangos database concept possible?

fechnert
  • 1,215
  • 2
  • 12
  • 30

1 Answers1

1

You can create an intermediate table and use the through argument of the ManyToMany field:

class Event(models.Model):
    ...
    vehicles = models.ManyToManyField(Vehicle, through='EventVehicle')

class EventVehicle(models.Model):
    event = models.ForeignKey(Event)
    vehicle = models.ForeignKey(Vehicle)
    loaded = models.IntegerField(default=2)
catavaran
  • 44,703
  • 8
  • 98
  • 85