views:

49

answers:

1

I'm trying to make a custom backend for my system and I've hit a bit of a snag....I want to give users the ability to add new makes/models/series that are not already in the system via a form. I'm wondering how I'll go about this...my models look as below:

class Manufacturer(models.Model):
  MANUFACTURER_POPULARITY_CHOICES = (
    ('1', 'Primary'),
    ('2', 'Secondary'),
    ('3', 'Tertiary'),
  )

  manufacturer = models.CharField(max_length=15, blank=False)
  date_added = models.DateField()
  manufacturer_popularity = models.CharField(max_length=1,
      choices=MANUFACTURER_POPULARITY_CHOICES)
  def __unicode__(self):
    return self.manufacturer

class Model(models.Model):
  model = models.CharField(max_length=20, blank=False)
  manufacturer = models.ForeignKey(Manufacturer)
  date_added = models.DateField()
  def __unicode__(self):
    name = ''+str(self.manufacturer)+" "+str(self.model)
    return name 

class Series(models.Model):
  series = models.CharField(max_length=20, blank=True, null=True)
  model = models.ForeignKey(Model)
  date_added = models.DateField()
  def __unicode__(self):
    name = str(self.model)+" "+str(self.series)
    return name

class Engine(models.Model):
  ENGINE_TYPE_CHOICES = (
    ('H', 'H'),
    ('I', 'I'),
    ('R', 'R'),
    ('V', 'V'),
    ('W', 'W'),
  )

  FUEL_TYPE_CHOICES = (
    ('G', 'Gas'),
    ('D', 'Diesel'),
  )

  size = models.DecimalField(max_digits=2, decimal_places=1)
  type = models.CharField(max_length=1, choices=ENGINE_TYPE_CHOICES)
  cylinders = models.PositiveSmallIntegerField() 
  spec = models.CharField(max_length=20, blank=True, null=True)
  fuel_type = models.CharField(max_length=1, choices=FUEL_TYPE_CHOICES)

class CommonVehicle(models.Model):
  year = models.ForeignKey(Year)
  series = models.ForeignKey(Series)
  engine = models.ForeignKey(Engine)
  body_style = models.ForeignKey(BodyStyle)
  transmission = models.ForeignKey(Transmission)
  speeds = models.PositiveSmallIntegerField()
  drive_train = models.ForeignKey(DriveTrain)
  horse_power = models.PositiveSmallIntegerField()
  litre_100km_city = models.DecimalField(max_digits=3, decimal_places=1) 
  litre_100km_hwy = models.DecimalField(max_digits=3, decimal_places=1)
  def __unicode__(self):
    name = ''+str(self.year)+" "+str(self.series)
    return name 
+2  A: 

This seems to be a fairly standard job for a django model form. I would recommend following the documentation at http://docs.djangoproject.com/en/dev/topics/forms/modelforms/. There are detailed instructions there on how to create a form from a model and then save the returned submission to the database.

Clueless
I think I've gotten a way around this...I'm already fairly conversant with the model form so thank you.
Stephen
Hmm, I'm wondering if there was a deeper issue you were concerned about in the question you posed. It seemed like far too intelligent and detailed a question to be easily answered by saying "follow the documentation" and yet it appears you have relatively nicely normalized data and a model well-suited to model forms.
Clueless