0

Hello I have simple model:

class Company(models.Model):
    name = models.CharField(max_length=150, unique=True, blank=False)
    fullname = models.CharField(max_length=250, blank=True, null=True)
    description = models.TextField(blank=True, null=False)
    ...
    

And I created simple test:

    ...
    def test_create_company_fail_name(self):
    """ Why create empty name Models!"""
        payload = {
        'fullname': 'Test2Django',
        'description': 'Simple test',
        'country': 'Poland',
        'city': 'Gdynia'
        }
        company = Company.objects.create(**payload)
        self.assertEqual(Company.objects.count(), 1)

Why django create model if it didn't get field 'name'? But when I create model in admin section then I get error 'Field is require'.

Mateusz
  • 13
  • 6
  • Please see the accepted answer for [this question](https://stackoverflow.com/questions/51148893/object-created-even-if-field-was-required). – evergreen May 31 '22 at 18:55

1 Answers1

0

There are two things

  1. Blank
  2. null

if blank is true so while filing form we can ignore that field

if null is true it means While inserting entry in DB if we are not passing that filed value then by default django will put null there.

So in your case blank is false it means from Admin panel UI it is mandatory to give some value for name. it will not give any error if we change blank=True in model

abhishek Singh
  • 332
  • 2
  • 7