Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 487
bigmike7801 Avatar asked Sep 07 '11 22:09

bigmike7801


People also ask

How do you make a form field not required?

Just add blank=True in your model field and it won't be required when you're using modelforms .

How do I make fields required in Django admin?

Just define a custom form, with your required field overridden to set required=True, and use it in your admin class.


2 Answers

Just Put

blank=True 

in your model i.e.:

rushing_attempts = models.CharField(         max_length = 100,         verbose_name = "Rushing Attempts",         blank=True         ) 
like image 159
fabrizioM Avatar answered Oct 16 '22 17:10

fabrizioM


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         ) 
like image 29
Dawn T Cherian Avatar answered Oct 16 '22 18:10

Dawn T Cherian