0

私はすべての「InterityError」+「maynobeNULL」の投稿を読みましたが、それでもこのエラーの原因を突き止めることができません。

2部構成の申し込みフォームがあります。最初の部分は、製品を選択することです。これにより、製品IDがURLの一部として次のページに渡され、そこで個人情報が入力されます。一部のフィールドを表示する必要がないため、フィールドの削除を開始するまで(モデルフォームを使用しています)、フォームを正常に機能させることができます。

これが私のモデルとmodelFormです。

class SimpleSubscriber(models.Model):
    name = models.CharField(max_length=255)
    address = models.CharField(max_length=200)
    city = models.CharField(max_length=100)
    state = models.CharField(max_length=2)
    zipcode = models.CharField(max_length=9)
    phone = models.CharField(max_length=10)
    email = models.EmailField()
    date_created = models.DateTimeField(null=True)
    sub_type = models.ForeignKey(Product)
    def __unicode__(self):
        return self.name


class SubscriberForm(ModelForm):
    class Meta:
        model = SimpleSubscriber
        fields = ('name', 'address', 'city', 'state', 'zipcode', 'phone', 'email', 'sub_type',)#'date_created', 

そして、これが私の見解です:

def select_product(request):
    title = "get yourself an e-edition. wurd."
    pform = Product.objects.order_by('product_active')  
    if request.method == 'POST': # If the form has been submitted...
        pform = ProductForm(request.POST) # A form bound to the POST data
        if pform.is_valid(): # All validation rules pass 
        # ...
            return HttpResponseRedirect('signup/%i' % pform.id) # Redirect after POST
    else:
        form = ProductForm() # An unbound form
    return render_to_response('signup/index.html', {'title': title, 'pform': pform}, context_instance=RequestContext(request))


def subscriber_signup(request, product_id):
    productchoice = Product.objects.get(id=product_id)
    now = datetime.datetime.now()
    title = "We need some information."  
    if request.method == 'POST': # If the form has been submitted...
        sform = SubscriberForm(request.POST) # A form bound to the POST data
        if sform.is_valid(): # All validation rules pass
            sform.date_created = now
            sform.sub_type = productchoice
            sform.save()
            return HttpResponseRedirect('thankyou/') # Redirect after POST
    else:
        sform = SubscriberForm() # An unbound form
    return render_to_response('signup/detail.html', {'title': title, 'sform': sform, 'productchoice': productchoice, 'now': now.date(),}, context_instance=RequestContext(request))

modelFormと関係があると思いますが、私はかなり新しいので、本当にわかりません。すべてのフィールドをSubscriberFormに追加すると、それらが入力され、すべてが正常に機能します。ただし、ユーザーがフォームに入力するときにユーザーが言う必要はないので、sform.date_created = nowと入力し、前のページで選択した選択肢によってproduct_idが自動的に入力されるようにします。ただし、これらのフィールドをフォームから除外すると、IntegrityErrorがスローされます。これは、何を変更するかを説明するのにあまり役立ちません。

私がめちゃくちゃになっている場所についてのヒントはありますか?

ありがとう、

4

2 に答える 2

1

2つのこと:

1) フォーム定義で exlude を使用することでメリットが得られる場合があります。

class SubscriberForm(ModelForm):
    class Meta:
        model = SimpleSubscriber
        exclude = ('date_created', ) 

2)あなたの質問に対して、それを修正する方法は次のとおりです。

if sform.is_valid(): # All validation rules pass

    suscriber = sform.save(commit=False)
    suscriber.date_created = now
    suscriber.sub_type = productchoice
    suscriber.save()
于 2012-04-13T04:27:20.140 に答える
0

@fceruti の提案の代わりに、必要null=Trueに応じてモデルのフィールドにさらに kwarg タグを追加することもできます。フォームで最小限のフィールド セットのみを強制的に入力します。

于 2013-02-07T19:17:54.157 に答える