好的,我有一个“模块”模型,这个模型涵盖了我可以用它创建的4种不同类型模块的所有字段。
其中一个模块需要ManyToManyField,因此我可以存储最近的付款,然后将它们循环到模板中。
以下是模块的模型:
class MarketModules(models.Model):
types = (
('1', 'Server Status'),
('2', 'Recent Donations'),
('3', 'Donation Goal'),
('4', 'Textbox')
)
id = models.AutoField("Module id", primary_key=True, editable=False)
owner = models.ForeignKey(User, blank=True, null=True)
marketid = models.ForeignKey(Market, blank=True, null=True)
type = models.CharField("Module type", choices=types, max_length=100, default=1, blank=True, null=True)
order = models.IntegerField("Module order", default=1, blank=True, null=True)
title = models.CharField("Module title", max_length=200, default="Module")
text = models.CharField("Text", max_length=3000, blank=True, null=True)
goal = models.IntegerField("Monthly goal",default=100, blank=True, null=True)
goalpercentage = models.IntegerField("Monthly percentage", default=100, blank=True, null=True)
goalamount = models.IntegerField("Monthly amount", default=0, blank=True, null=True)
maxDisplay = models.IntegerField("Display amount",default=5, blank=True, null=True)
recentdonors = models.ManyToManyField(ItemsBought, null=True, blank=True)
ip = models.CharField("Server IP", max_length=100, blank=True, null=True)
port = models.IntegerField("Server port", max_length=20, default=25565, blank=True, null=True)
players = models.IntegerField("Players Online", max_length=20, blank=True, null=True)
playerpercentage = models.IntegerField("Player percentage", default=100, blank=True, null=True)
maxplayers = models.IntegerField("Max Players", max_length=20, blank=True, null=True)
playersnames = models.TextField("Player Names", max_length=5000, blank=True, null=True)
online = models.BooleanField("Online", default=0)
motd = models.CharField("MOTD", blank=True, null=True, max_length=250)
enabled = models.BooleanField(default=True)
def __unicode__(self):
return'%s' % self.id
以下是编辑表单的Views.py:
if request.method == "POST":
newmodule = ModuleForm(request.POST, instance=instance)
if newmodule.is_valid():
module = newmodule.save()
if module.type == '1':
query_server(MarketModules.objects.filter(pk=module.id))
elif module.type == '3':
CalGoal(MarketModules.objects.filter(pk=module.id), module.goal)
elif module.type == '2':
RecentDonors(module.id, module.maxDisplay)
return redirect("/controlpanel/modules/")
else:
newmodule = ModuleForm(instance=instance)
,这是ModuleForm的forms.py:
class ModuleForm(forms.ModelForm):
text = forms.CharField(required=False, widget=CKEditorWidget(attrs={'class': 'form-control'}))
title = forms.CharField(required=False, widget=forms.TextInput(attrs={'class': 'form-control'}))
ip = forms.CharField(required=False, widget=forms.TextInput(attrs={'class': 'form-control'}))
port = forms.CharField(required=False, widget=forms.TextInput(attrs={'class': 'form-control'}))
maxDisplay = forms.CharField(required=False, widget=forms.TextInput(attrs={'class': 'form-control'}))
goal = forms.CharField(required=False, widget=forms.TextInput(attrs={'class': 'form-control'}))
class Meta:
model = MarketModules
def save(self, commit=True, owner=None, marketid=None, order=None, type=None):
modelform = super(ModuleForm, self).save(commit=False)
if type:
modelform.type = type
if order:
modelform.order = order
if owner:
modelform.owner = owner
if marketid:
modelform.marketid = marketid
if commit:
modelform.save()
return modelform
问题是即使ManytoMany字段应该接受空白进行验证,并且在保存时应该被允许为空我不断收到错误
invalid literal for int() with base 10: ''
如果我删除blank=True
和null=True
,我只会收到验证错误,说明其按预期要求。
如何将ManyToMany字段保存为空?
根据要求进行跟踪:
Traceback:
File "C:\Python27\Lib\site-packages\django\core\handlers\base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "C:\Users\xXxKillDogxXx\Desktop\MinecraftMarket-Redesign\market\views.py" in EditModule
617. module = newmodule.save()
File "C:\Users\xXxKillDogxXx\Desktop\MinecraftMarket-Redesign\market\forms.py" in save
99. modelform.save()
File "C:\Python27\Lib\site-packages\django\db\models\base.py" in save
546. force_update=force_update, update_fields=update_fields)
File "C:\Python27\Lib\site-packages\django\db\models\base.py" in save_base
626. rows = manager.using(using).filter(pk=pk_val)._update(values)
File "C:\Python27\Lib\site-packages\django\db\models\query.py" in _update
605. return query.get_compiler(self.db).execute_sql(None)
File "C:\Python27\Lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
1014. cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "C:\Python27\Lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
830. sql, params = self.as_sql()
File "C:\Python27\Lib\site-packages\django\db\models\sql\compiler.py" in as_sql
979. val = field.get_db_prep_save(val, connection=self.connection)
File "C:\Python27\Lib\site-packages\django\db\models\fields\__init__.py" in get_db_prep_save
304. prepared=False)
File "C:\Python27\Lib\site-packages\django\db\models\fields\__init__.py" in get_db_prep_value
296. value = self.get_prep_value(value)
File "C:\Python27\Lib\site-packages\django\db\models\fields\__init__.py" in get_prep_value
991. return int(value)
Exception Type: ValueError at /controlpanel/modules/980/1/edit/
Exception Value: invalid literal for int() with base 10: ''
答案 0 :(得分:2)
问题是,您的ModuleForm
宣布goal
为CharField
。你告诉Django这应该是一个字符串,所以当没有输入一个值时,空字符串(''
)会被传递给数据库层,这就是期望一个整数。
解决方案很简单:goal = forms.IntegerField(...)
。请确保对其他字段执行相同操作(例如port
,maxDisplay
)。