如何从特定表格创建表单并将其保存到不同的表

时间:2015-02-10 10:26:20

标签: python django forms

根据我的知识,每当我们制作表格时,它都会从表格中提取表格并将其保存到同一张表格中。 例如:

class ExampleForm(forms.Form):
     class Meta:
          model=Example

此处ExampleFormExample模型构成,一旦提交,它就会保存回Example模型。 我想要的是,我想从表中开发一个表单并将其保存到另一个表中? 这可能吗?

为什么我要这个?

因为,我正在创建一个表单,只包含复选框,并且从表中检索这些复选框的标签,但我想将用户输入保存到不同的表中

models.py

class Offer(models.Model):

    package = models.ForeignKey(Package)
    discount = models.CharField(max_length=120,null=True,blank=True)
    transportation = models.CharField(max_length=120,null=True,blank=True)
    nextTrip = models.CharField(max_length=120,null=True,blank=True)
    rewardPoints = models.IntegerField(max_length=3,null=True,blank=True)
    refferals = models.CharField(max_length=3, choices=REFERRAL_CHOICES)

    def __str__(self):
        return self.package.packageTitle

Offer模型用于创建表单,但提交数据后,应将其保存在OfferRequest模型中

class OfferRequest(models.Model):   
    offerReq = models.ForeignKey(Offer,null=True)
    userReq = models.ForeignKey(User)
    discountReq = models.BooleanField(default=False)
    transportationReq = models.BooleanField(default=False)
    nextTripReq = models.BooleanField(default=False)
    rewardPointsReq = models.BooleanField(default=False)
    refferalsReq = models.BooleanField(default=False)

    def __str__(self):
        return self.discountReq

forms.py

class OfferRequestForm(forms.ModelForm):
    discountReq = forms.BooleanField(required=False,label="Discount")
    transportationReq = forms.BooleanField(required=False,label="Transportation")
    nextTripReq = forms.BooleanField(required=False,label="Next Trip")
    rewardPointsReq = forms.BooleanField(required=False,label="Reward Points")
    refferalsReq = forms.BooleanField(required=False,label="Refferals")
    class Meta:
        model = OfferRequest
        fields = ("discountReq","transportationReq","nextTripReq","rewardPointsReq","refferalsReq")

由于我从OfferRequest制作表单,因此呈现方式如下所示:

  

()折扣
  ()运输
  ()nextTrip
  ()refferals
  ()rewardPoints

但我想要的是:

  

()折扣:3%
  ()运输:免费
  ()nextTrip:20%
  ()refferals:是
  ()rewardPoints:200

如果表单是从Offer模型派生的,则可以呈现上面的表单输出,因为Offer模型中定义了不同的商品

现在,当提交此表单(源自Offer)时,我希望将其保存在OfferRequest


尽我所能解释,请问是否有不明确的事情。

1 个答案:

答案 0 :(得分:0)

如果您要将Offer的值添加到OfferRequestForm的字段标签中,则应覆盖__init__()方法:

class OfferRequestForm(forms.ModelForm):
    ...
    def __init__(self, *args, **kwargs):
        offer = kwargs.pop('offer')
        super(OfferRequestForm, self).__init__(*args, **kwargs)
        for field in offer._meta.fields:
            field_name = field.name
            form_field_name = field_name + 'Req'
            if form_field_name in self.fields:
                self.fields[form_field_name].label += \
                                u': %s' % getattr(offer, field_name)

所以现在你必须将offer参数传递给OfferRequestForm的构造函数:

def offer_req(request, offer_id):
    offer = get_object_or_404(Offer, pk=offer_id)
    if request.method == 'POST':
        form  = OfferRequestForm(request.POST, offer=offer)
        if form.is_valid():
            offer_request = form.save(commit=False)
            offer_request.offerReq = offer
            offer_request.userReq = request.user
            offer_request.save()
            return redirect(offer_request)
    else:
        form  = OfferRequestForm(offer=offer)
    return render(request, 'offer_req.html', {'form': form})