我希望我的外键值等于用户选择的值

时间:2019-09-10 07:08:20

标签: django django-models django-views

好日子,在我的模型中,我的外键关系需要帮助, 我希望我的外键值等于用户选择的值

这是我的模型视图

class ProjectNameInviToBid(models.Model):
    ProjectName = models.CharField(max_length=255, verbose_name='Project Name', null=True)
    DateCreated = models.DateField(auto_now=True)

    def __str__(self):
        return self.ProjectName

与ProjectNameInviToBid有关系

class InviToBid(models.Model):
    today = date.today()
    ProjectName = models.ForeignKey('ProjectNameInviToBid', on_delete=models.CASCADE, null=True)
    NameOfFile = models.CharField(max_length=255, verbose_name='Name of File')
    Contract_No = models.IntegerField(verbose_name='Contract No')
    Bid_Opening = models.CharField(max_length=255, verbose_name='Bid Opening')
    Pre_Bid_Conference = models.CharField(max_length=255, verbose_name='Pre Bid Conference')
    Non_Refundable_Bidder_Fee = models.CharField(max_length=255, verbose_name='Non Refundable Fee')
    Delivery_Period = models.CharField(max_length=255, verbose_name='Delivery Period')
    Pdf_fileinvi = models.FileField(max_length=255, upload_to='upload/invitoBid', verbose_name='Upload Pdf File Here')
    Date_Upload = models.DateField(auto_now=True)

这是我的表单视图

class invitoBidForm(ModelForm):

    class Meta:

        model = InviToBid
        fields = ('ProjectName','NameOfFile', 'Contract_No', 'Bid_Opening',
                  'Pre_Bid_Conference', 'Non_Refundable_Bidder_Fee',
                  'Delivery_Period',
                  'Pdf_fileinvi')

最后,这是我的视图表单

def project_name_details(request, sid):

    majordetails = ProjectNameInviToBid.objects.get(id=sid)

    if request.method == 'POST':
        form = invitoBidForm(request.POST, request.FILES)
        if form.is_valid():
            majordetails = form.ProjectName
            form.save()
            messages.success(request, 'File has been Uploaded')
    else:
        form = invitoBidForm()

    args = {
        'majordetails': majordetails,
        'form': form

    }
    return render(request,'content/invitoBid/bacadmininvitoBid.html', args)

但是我有一个错误,希望您能帮助我。预先谢谢你

0 个答案:

没有答案