This is my models.py file which uses AutoField for post_id which is a primary key so it is unique. AutoField does auto incrementing as I have read.
from django.db import models
class Post(models.Model):
post = models.CharField(max_length=1000,unique=True)
post_id = models.AutoField(primary_key=True)
neg = models.IntegerField(default=0)
pos = models.IntegerField(default=0)
is_money= models.CharField(max_length=1,default = "y")
def __str__(self):
return self.post+"\tScore : "+str(self.neg+self.pos)
I have a startup.py file which adds records to db using the following code. I have used get_or_create
function to check if the post exits else create it Post.objects.get_or_create(post = text ,defaults={'neg':0,'pos':0,'is_money':has_money_tags})
def add_to_db(self):
from reviewer.models import Post
Post.objects.all().delete()
import django
django.setup()
for num,i in enumerate(self.unlabelled):
print num
money_tags=self.getHashTagsfromContent(i[0])
text = re.sub(r'#[^\s]+',"",i[0])
y_n=i[1] if i[1].strip()=='y' or i[1].strip()=='n' else filter(None,map(lambda x : x.strip("]'[ \n\t"),i[1].split(",")))
if isinstance(y_n,list):
money_tags=money_tags+y_n
has_money_tags=y_n if y_n =='y' or y_n=='n' else self.isMoney(money_tags)
self.logger.info(str(text)+"\t"+str((num+1))+"\t"+str(has_money_tags)+"\n")
p,created=Post.objects.get_or_create(post = text ,defaults={'neg':0,'pos':0,'is_money':has_money_tags})
def run():
r=Reviewer_Data()
r.add_to_db()
This is the Migrations class being created after makemigrations
class Migration(migrations.Migration):
dependencies = [
]
operations = [
migrations.CreateModel(
name='Post',
fields=[
('post', models.CharField(unique=True, max_length=1000)),
('post_id', models.AutoField(serialize=False, primary_key=True)),
('neg', models.IntegerField(default=0)),
('pos', models.IntegerField(default=0)),
('is_money', models.CharField(default=b'y', max_length=1)),
],
),
]
When I execute python manage.py runserver
. It gives me an error for the column post_id django.db.utils.IntegrityError: NOT NULL constraint failed: reviewer_post.post_id
I am trying to add some records in the database during server start and it worked when there were no unique fields but when I updated my models file to make two fields unique post and post_id it started having problems with post_id.
How can I make sure adding a unique field to work?
Any help is appreciated.