119

Every time I enter in a new player in the Admin portion of Django I get an error message that says "This field is required.".

Is there a way to make a field not required without having to create a custom form? Can I do this within models.py or admin.py?

Here's what my class in models.py looks like.

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"
        )

Thanks

bigmike7801
  • 3,908
  • 9
  • 49
  • 77
  • 5
    The simplest way is by using the field option blank=True (https://docs.djangoproject.com/en/dev/ref/models/fields/#blank). Is there a reason why that won't work? – Platinum Azure Sep 07 '11 at 22:13

3 Answers3

203

Just Put

blank=True

in your model i.e.:

rushing_attempts = models.CharField(
        max_length = 100,
        verbose_name = "Rushing Attempts",
        blank=True
        )
fabrizioM
  • 46,639
  • 15
  • 102
  • 119
  • Be aware that if you use "forms", the blank=true will not work. E.g. here the blank= true from the model will not work: class MusModelForm( forms.ModelForm ): name = forms.CharField( widget=forms.Textarea ) #~ mitglieder = forms.CharField( widget=forms.Textarea ) class Meta: model = Musician – Timo May 01 '14 at 06:50
  • 2
    If the field is set blankable in model level, it really means empty string are allowed. An empty string and a null really aren't the same thing. Don't break your data integrity only because the framework has set some bad default features. Instead of setting the blank, override the get_form -method: https://stackoverflow.com/a/70212909/784642 – niko.makela Dec 03 '21 at 10:34
12

Use blank=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
        )
Dawn T Cherian
  • 4,968
  • 3
  • 24
  • 35
  • 4
    You should not need "null=True" on CharFields at least from Django 1.6 forward, probably even earlier. Similarly for TextField, SlugField, EmailField, ... anything that is stored as text. – jenniwren Sep 01 '16 at 00:46
  • Django doesn't recommend "null=True" for fields strictly containing text. – kas Dec 15 '16 at 02:12
  • It is the complete answer. Thanks for posting. – Siraj Alam Apr 22 '18 at 11:19
  • I can't imaging why you will recommend against making Text Field Null, can you cite where this was mentioned on Django Doc? – Paullo Jul 24 '18 at 13:21
  • 1
    @Paullo "Avoid using null on string-based fields such as CharField and TextField. If a string-based field has null=True, that means it has two possible values for “no data”: NULL, and the empty string. In most cases, it’s redundant to have two possible values for “no data;” the Django convention is to use the empty string, not NULL. One exception is when a CharField has both unique=True and blank=True set. In this situation, null=True is required to avoid unique constraint violations when saving multiple objects with blank values." https://docs.djangoproject.com/en/2.1/ref/models/fields/#null – Masood Khaari Dec 22 '18 at 09:37
  • 1
    Although the rationale is unconvincing to me. :) – Masood Khaari Dec 22 '18 at 09:40
  • 1
    @MassoodKhaari I see what you mean and I agree with you that "rationale is unconvincing". – Paullo Dec 23 '18 at 08:36
5

If the field is set blankable in model level, it really means empty string are allowed. An empty string and a null really aren't the same thing. Don't break your data integrity only because the framework has set some bad default features.

Instead of setting the blank, override the get_form -method:

    def get_form(self, request, obj=None, **kwargs):
        form = super().get_form(request, obj, **kwargs)
        form.base_fields["rushing_attempts"].required = False
niko.makela
  • 537
  • 3
  • 14
  • @Conor, I don't think a new comer who doesn't know what super() does should be concerned with even Django, not to talk of all its OOP things. – Qausim Aug 26 '22 at 13:16