Django IntegrityError signup_simplesubscriber.date_created可能不是NULL

时间:2012-04-13 03:39:22

标签: django django-forms

我已经阅读了每个“InterityError”+“可能不是NULL”的帖子,仍然无法追踪导致此错误的原因。

我有一个两部分的注册表格。第一部分是选择产品。这会将产品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,这对解释要更改的内容没有多大帮助。

有关我搞砸的地方的任何提示?

谢谢,

2 个答案:

答案 0 :(得分:1)

两件事:

1)您可以从表单定义中使用exclude中受益:

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()

答案 1 :(得分:0)

除了@ fceruti的建议外,您还可以在模型的字段中添加更多kwarg标记null=True - 仅强制在表单中填写最少的字段集。