So I have a Django model which has few non nullable field with default values.
class Article(models.Model):
id = models.UUIDField(default=uuid4, primary_key=True, editable=False)
code = models.CharField(max_length=200, default=uuid4, unique=True, null=False)
company = models.CharField(max_length=200, default=uuid4, unique=True, null=False)
Now i want to do a
Article.objects.get_or_create(company=company, code=am_row['Article code'])
But here the issue is am_row['Article code'] or company can be a None, and in that case i just want a new model to be created using that fields default values.
Currently passing None throws a NOT NULL constraint failed error. How can i achieve this?
So i think this boils down to achieving
Article.objects.create(company=company, code=am_row['Article code'])
obj = Foo.objects.create() obj.save()? That should use the default values. You could also check if they are none and just pass in the default values