English 中文(简体)
Django模式表格中添加外地内容,造成廉正 另一领域错误
原标题:Adding field in Django Model Formset causes IntegrityError on another field

因此,我有一个模式,我使用示范工厂。

如果我离开这种表格,我就没有错误:

class CheckinForm(ModelForm):

class Meta:
    model = Checkout
    fields = ( return_date , )


def __init__(self, *args, **kwargs):
    super(CheckinForm, self).__init__(*args, **kwargs)
    self.fields[ return_date ].widget = CheckboxInput()

如果我增列另一个领域,即extension to the fields,则我收到(1048,“Column book_id not bern”,BUT,这些改动将节省到数据库。

我的模式如下:

class Checkout(models.Model):
book = models.ForeignKey(Book)
user = models.ForeignKey(User)
checkout_date = models.DateField(auto_now_add = True)
return_date = models.DateField(null = True, blank=True)
extension = models.IntegerField("Extension in days", blank = True, default = 0)

并在此处理形式:

def checkin(request):
c = RequestContext(request, dictionary)
CheckinFormSet = modelformset_factory(Checkout, CheckinForm)
if request.method == "POST":
    data = request.POST.copy()
    for i in range(0, int(data[ form-TOTAL_FORMS ])):
        if  form-  + str(i) +  -return_date  in data:
            data[ form-  + str(i) +  -return_date ] = datetime.date.today().isoformat()
        else:
            data[ form-  + str(i) +  -return_date ] =   

    formset = CheckinFormSet(data = data)
    user_form = AutoUserForm(data = data)
    if formset.is_valid():
        c[ cool ] =  cool 
        formset.save()
    else:
        c[ err ] = formset.errors
        c[ data ] = data
else:
    CheckinFormSet = modelformset_factory(Checkout, CheckinForm)
    user_form = AutoUserForm()
c[ user_form ] = user_form
c[ form ] = CheckinFormSet
c[ context ] =  checkin 
return render_to_response( lib_admin/checkin.html , {}, c)

我不得不通过表格数据,因为对于<代码>return_date<>/code>的外地,我刚刚有一个用户将打上标的核对箱,而现在又加上这个日期。

This seems really weird to me, especially because the data is saved, even though I receive the IntegrityError.

最佳回答

使用<代码>空白=True<>/code>和default就是一个好的想法。 它要么具有价值(过失),要么没有(空白),但不是同时存在。

解决办法:

extension = models.IntegerField("Extension in days", default=0)
问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签