모든 "InterityError"+ "NULL이 아닐 수 있습니다"게시물을 읽었지만 여전히이 오류의 원인을 추적 할 수 없습니다.Django IntegrityError signup_simplesubscriber.date_created가 NULL이 아니어도됩니다.
두 부분으로 구성된 가입 양식을 받았습니다. 첫 번째 부분은 제품을 선택하는 것입니다. 그러면 URL의 일부로 제품 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 =를 입력하면 이전 페이지에서 선택한 항목에 따라 product_id가 자동으로 채워지 길 원합니다. 그러나 양식에서이 필드를 제외하면 IntegrityError가 발생하므로 변경해야 할 부분을 설명하는 데별로 도움이되지 않습니다.
내가 엉망이되는 부분에 대한 힌트가 있습니까?
감사합니다,
정말 대단합니다. 확실히 오류를 수정했습니다. 왜 커밋 = 거짓인가? 그 일이 무엇이며,이 명령의 순서가 중요합니까? 나는 sform.save (commit = False)가 먼저 가야하고 subscriber.save()가 마지막이라고 가정 할 것인가? –
폼의 save 메소드의 작업은 데이터를 처리하고 객체를 반환하는 것입니다. 커밋하지 않는다고 말하면, 데이터베이스 동기화 부분 없이는 그렇게 할 수 있습니다. – fceruti