需要另一个应用程序中的模型的Django形式的下拉列表

时间:2019-12-01 19:35:01

标签: django django-models django-forms

我在1)boq 2)inputs中有两个应用程序 输入具有模型构建

class building(models.Model):

    building = models.CharField(max_length=300

我的boq应用程序具有模型boqmodel

class boqmodel(models.Model):
    code = models.IntegerField()
    building =models.CharField(max_length=300)
    level = models.CharField(max_length=300)
    activity = models.CharField(max_length=300)
    subactivity = models.CharField(max_length=300)
    duration=models.IntegerField()
    linkactivity= models.CharField(max_length=300)
    linktype= models.CharField(max_length=300)
    linkduration=models.IntegerField()
    plannedstart=models.DateField()
    plannedfinish=models.DateField()
    actualstart=models.DateField()
    actualfinish=models.DateField()

我在boq应用程序中有一个表单,如下所示

class boqform(forms.ModelForm):
    class Meta:
        model = boqmodel
        fields = ['code',
                  'building',
                  'level',
                  'activity',
                  'subactivity',
                  'duration',
                  'linkactivity',
                  'linktype',
                  'linkduration',
                  'plannedstart',
                  'plannedfinish',
                  'actualstart',
                  'actualfinish']

Build Bolou和Boq和Inputs应用程序相同 我需要使用boq形式进行下拉,以便使用inputs.building模型中的模型值进行构建

1 个答案:

答案 0 :(得分:0)

class building(models.Model):
    building = models.CharField(max_length=300)

    def __str__(self):
        return self.building

class boqmodel(models.Model):
    code = models.IntegerField()
    #building =models.CharField(max_length=300)
    building = models.ForeignKey(building, on_delete=models.SET_NULL, null=True)
    level = models.CharField(max_length=300)
    activity = models.CharField(max_length=300)
    subactivity = models.CharField(max_length=300)
    duration = models.IntegerField()
    linkactivity = models.CharField(max_length=300)
    linktype = models.CharField(max_length=300)
    linkduration = models.IntegerField()
    plannedstart = models.DateField()
    plannedfinish = models.DateField()
    actualstart = models.DateField()
    actualfinish = models.DateField()

完成