ChatGPT解决这个技术问题 Extra ChatGPT

Can I make an admin field not required in Django without creating a form?

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

The simplest way is by using the field option blank=True (docs.djangoproject.com/en/dev/ref/models/fields/#blank). Is there a reason why that won't work?

f
fabrizioM

Just Put

blank=True

in your model i.e.:

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

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
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: stackoverflow.com/a/70212909/784642
D
Dawn T Cherian

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
        )

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.
Django doesn't recommend "null=True" for fields strictly containing text.
@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." docs.djangoproject.com/en/2.1/ref/models/fields/#null
Although the rationale is unconvincing to me. :)
@MassoodKhaari I see what you mean and I agree with you that "rationale is unconvincing".
n
niko.makela

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

not answered enough - explain how it is used practically, why using super()? Newcomers to django can't understand the answer

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now