0

This is my code:

class Order(TimeStampedModel):
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    merchant_uid = models.CharField(max_length=30, unique=True)
    imp_uid = models.CharField(max_length=30)
    from_cart = models.BooleanField()

    class Meta:
        ordering = ('-created',)

    def __str__(self):
        return self.merchant_uid

But strange thing is,

  1. Order.objects.create(user=request.user, from_cart=True) works (in views.py).

  2. order = Order(user=request.user, from_cart=True) and order.save() also works.

I didn't set blank=True and null=True on my merchant_uid, imp_uid fields, which means required field.

But how is it possible to create model without those field??

user3595632
  • 5,380
  • 10
  • 55
  • 111
  • 2
    Possible duplicate of [differentiate null=True, blank=True in django](http://stackoverflow.com/questions/8609192/differentiate-null-true-blank-true-in-django) – timo.rieber Oct 11 '16 at 08:26

1 Answers1

1

The values are left empty. Because null=False, Django doesn't store empty values as null for these fields, and instead the database uses the empty string ('') for a varchar column that doesn't get a value.

blank is used for validation, e.g. when validating a ModelForm. It is not related to database constraints. You don't do any validation here, so it's not relevant.

RemcoGerlich
  • 30,470
  • 6
  • 61
  • 79