I'm trying to create sample data for my project, and I'm using faker to populate my database. The customer model i'm using has a field for tags, which is a comma separated list of values. For my faker script, I'm created a list and broke it down into a comma separated list, but the database won't except the value. Here is my code:
import os
# Configure settings for project
# Need to run this before calling models from application!
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'openroad.settings')
import django
# Import settings
django.setup()
from random import randint, choice
from dispatch.models import Customer
from faker import Faker
fakegen = Faker()
my_word_list = [
'danish','cheesecake','sugar',
'Lollipop','wafer','Gummies',
'sesame','Jelly','beans',
'pie','bar','Ice','oat',
'group', 'octopus', 'noble',
'crocodile', 'gravel', 'excellent', 'destiny',
'bleeding', 'phenomena', 'downward'
]
def populate(N=5):
for entry in range(N):
# Create Fake Data for entry
fake_company = fakegen.company()
fake_name = fakegen.name().split()
fake_poc = fake_name[0] + ' ' + fake_name[1]
fake_contact = randint(1111111111, 9999999999)
fake_street = fakegen.address().split('\n')
fake_city = fake_street[1].split(',')
fake_state = fake_city[1].split()
fake_zip = fake_state[1].split('-')
fake_notes = fakegen.text()
count = 0
tag_count = randint(1, 4)
tags = []
while count <= tag_count:
tags.append(choice(my_word_list))
count += 1
tags = ",".join(str(x) for x in tags)
customer = Customer.objects.get_or_create(name=fake_company,
address1=fake_street[0],
city=fake_city[0],
state=fake_state[0],
zipcode=fake_zip[0],
poc=fake_poc,
contact=fake_contact,
notes=fake_notes,
customer_tags=tags
)
if __name__ == '__main__':
print("Populating the databases...Please Wait")
populate(1)
print('Populating Complete')
Here is the traceback info:
Populating the databases...Please Wait
Traceback (most recent call last):
File "populate_customers.py", line 63, in <module>
populate(1)
File "populate_customers.py", line 58, in populate
customer_tags=tags
File "/home/jboucher/anaconda3/envs/openroad/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/jboucher/anaconda3/envs/openroad/lib/python3.6/site-packages/django/db/models/query.py", line 464, in get_or_create
return self.get(**lookup), False
File "/home/jboucher/anaconda3/envs/openroad/lib/python3.6/site-packages/django/db/models/query.py", line 371, in get
clone = self.filter(*args, **kwargs)
File "/home/jboucher/anaconda3/envs/openroad/lib/python3.6/site-packages/django/db/models/query.py", line 784, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/home/jboucher/anaconda3/envs/openroad/lib/python3.6/site-packages/django/db/models/query.py", line 802, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/home/jboucher/anaconda3/envs/openroad/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1261, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/home/jboucher/anaconda3/envs/openroad/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1287, in _add_q
allow_joins=allow_joins, split_subq=split_subq,
File "/home/jboucher/anaconda3/envs/openroad/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1217, in build_filter
condition = lookup_class(lhs, value)
File "/home/jboucher/anaconda3/envs/openroad/lib/python3.6/site-packages/django/db/models/lookups.py", line 24, in __init__
self.rhs = self.get_prep_lookup()
File "/home/jboucher/anaconda3/envs/openroad/lib/python3.6/site-packages/django/db/models/fields/related_lookups.py", line 112, in get_prep_lookup
self.rhs = target_field.get_prep_value(self.rhs)
File "/home/jboucher/anaconda3/envs/openroad/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 966, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'oat,octopus,sesame,bar,beans'
Model.py:
from taggit.managers import TaggableManager
class Customer(models.Model):
name = models.CharField(max_length=100)
address1 = models.CharField(max_length=100)
address2 = models.CharField(max_length=100, blank=True)
city = models.CharField(max_length=50)
state = models.CharField(max_length=2)
zipcode = models.CharField(max_length=5)
contact = models.CharField(max_length=15)
poc = models.CharField(max_length=100)
notes = models.TextField(blank=True, null=True)
customer_tags = TaggableManager()
def __str__(self):
return self.name
customer_tags
is a Taggit manager field, which under the hood is a many-to-many relationship; you can't set it directly on the model but you need to use the .add
method.
customer, _ = Customer.objects.get_or_create(name=fake_company,
address1=fake_street[0],
city=fake_city[0],
state=fake_state[0],
zipcode=fake_zip[0],
poc=fake_poc,
contact=fake_contact,
notes=fake_notes)
customer.customer_tags.add(tags)