2011-09-07 17 views
50

Ogni volta che inserisco un nuovo player nella sezione Admin di Django, ricevo un messaggio di errore che dice "Questo campo è richiesto".Posso creare un campo di amministrazione non richiesto in Django senza creare un modulo?

C'è un modo per rendere un campo non richiesto senza dover creare un modulo personalizzato? Posso farlo all'interno di models.py o admin.py?

Ecco come appare la mia classe in models.py.

class PlayerStat(models.Model): 
    player = models.ForeignKey(Player) 

    rushing_attempts = models.CharField(
     max_length = 100, 
     verbose_name = "Rushing Attempts" 
     ) 
    rushing_yards = models.CharField(
     max_length = 100, 
     verbose_name = "Rushing Yards" 
     ) 
    rushing_touchdowns = models.CharField(
     max_length = 100, 
     verbose_name = "Rushing Touchdowns" 
     ) 
    passing_attempts = models.CharField(
     max_length = 100, 
     verbose_name = "Passing Attempts" 
     ) 

Grazie

+2

Il modo più semplice è usare l'opzione di campo blank = True (https://docs.djangoproject.com/en/dev/ref/models/fields/#blank). C'è una ragione per cui non funzionerà? –

risposta

99

Basta mettere

blank=True 

nel modello cioè .:

rushing_attempts = models.CharField(
     max_length = 100, 
     verbose_name = "Rushing Attempts", 
     blank=True 
     ) 
+0

Ricorda che se usi "moduli", lo spazio vuoto = true non funzionerà. Per esempio. qui lo spazio vuoto = true dal modello non funzionerà: class MusModelForm (forms.ModelForm): name = forms.CharField (widget = forms.Textarea) # ~ mitglieder = forms.CharField (widget = forms.Textarea) classe Meta: model = Musician – Timo

3

Usa vuoto = True, null = True

class PlayerStat(models.Model): 
    player = models.ForeignKey(Player) 

    rushing_attempts = models.CharField(
     max_length = 100, 
     verbose_name = "Rushing Attempts", 
     blank=True, 
     null=True 
     ) 
    rushing_yards = models.CharField(
     max_length = 100, 
     verbose_name = "Rushing Yards", 
     blank=True, 
     null=True 
     ) 
    rushing_touchdowns = models.CharField(
     max_length = 100, 
     verbose_name = "Rushing Touchdowns", 
     blank=True, 
     null=True 
     ) 
    passing_attempts = models.CharField(
     max_length = 100, 
     verbose_name = "Passing Attempts", 
     blank=True, 
     null=True 
     ) 
+1

Non dovresti aver bisogno di "null = True" su CharFields almeno da Django 1.6 avanti, probabilmente anche prima. Allo stesso modo per TextField, SlugField, EmailField, ... tutto ciò che è memorizzato come testo. – jenniwren

+0

Django sconsiglia "null = True" per i campi che contengono rigorosamente testo. – kas

Problemi correlati