Reputation: 17299
How can I represent a column with a default value (specified in SQL) in a django model?
Table:
CREATE TABLE myTable
(
id SERIAL,
myGuid VARCHAR DEFAULT uuid_generate_v4(),
anotherColumn VARCHAR
)
Model:
class myTable(models.Model):
id = models.AutoField(primary_key=True)
#How to represent myGuid column
anotherColumn = models.CharField(max_length=40)
class Meta:
db_table = "myTable"
I'm using Python 2.7.3 and Django 1.4.2
Upvotes: 0
Views: 753
Reputation: 53386
Instead of defining it at DB level, you can use uuid from python as default value for the uuid field.
import uuid
def get_uuid():
return str(uuid.uuid4())
class myTable(models.Model):
id = models.AutoField(primary_key=True)
myuuid = models.CharField(max_length=40, unique=True, default=get_uuid)
anotherColumn = models.CharField(max_length=40)
class Meta:
db_table = "myTable"
Upvotes: 1