0

我已经阅读了每篇“InterityError”+“may no be 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,这对于解释要更改的内容没有多大帮助。

关于我在哪里搞砸的任何提示?

谢谢,

4

2 回答 2

1

两件事情:

1)您可能会受益于在您的表单定义中使用排除:

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 回答