2013-12-07 74 views
1

好吧,所以我有一個模塊「模塊」,這個模型涵蓋了我可以從它創建的4個不同類型的模塊的所有領域。Django manytomany字段無效字面值爲int()與基10:''

其中一個模塊需要一個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字段應該接受一個空白的驗證,應該允許爲空wh恩救我不斷收到錯誤

invalid literal for int() with base 10: '' 

如果我刪除blank=Truenull=True我只是得到一個驗證錯誤,並稱其爲預期的需要。

如何保存ManyToMany字段爲空?

TRACEBACK的要求:

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

你可以發佈完整的追溯? – aIKid

+0

添加追蹤:) –

+0

您確定問題是與M2M領域?你能否提供失敗的數據(表格)? – twil

回答

2

的問題是,你的ModuleForm正在申報goal是一個CharField。你告訴Django它應該是一個字符串,所以當一個值沒有輸入時,空字符串('')被傳遞到數據庫層,該層期望一個整數。

解決方案很簡單:goal = forms.IntegerField(...)。請確保您對其他字段的操作相同(例如portmaxDisplay)。

+0

Omg我覺得很愚蠢非常感謝你! –

相關問題