私はすべての「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がスローされます。これは、何を変更するかを説明するのにあまり役立ちません。
私がめちゃくちゃになっている場所についてのヒントはありますか?
ありがとう、