自定义Django表单

时间:2010-02-02 06:54:46

标签: django django-forms

我正在尝试为我的系统制作一个自定义后端,我遇到了一些麻烦....我想让用户能够添加尚未出现的新品牌/型号/系列系统通过表格。我想知道我将如何解决这个问题......我的模型如下所示:

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 

1 个答案:

答案 0 :(得分:2)

这似乎是django模型形式的一个相当标准的工作。我建议按照http://docs.djangoproject.com/en/dev/topics/forms/modelforms/的说明进行操作。有关于如何从模型创建表单然后将返回的提交保存到数据库的详细说明。