问题
I have 2 models Influencer and Category. Influencer has a many to many relationship with Category.The models look like this:
class Category(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
and
class Influencer(models.Model):
full_name = models.CharField('Full Name',max_length=100)
username = models.CharField('Username',max_length=100,unique=True)
photo = models.ImageField(upload_to = 'photos/%Y/%m/%d/',blank=True)
location_city = models.CharField('Location City',max_length=100,null=True,blank=True)
categories = models.ManyToManyField(Category)
I have written a python script that parses a csv file and sends the data to a PostgreSQL database.
In the csv file the column Category is present as an array, like the one given below
['Food', 'Bar', 'Bar', 'Student', 'Student', 'makeup', 'makeup', 'India', 'India']
A screenshot of the csv file
When I print the type of column Category in python it shows it as a string. The function which I wrote to parse and send data to database is as follows.I have excluded the categories option from the function for now.
def write_to_db(file):
with open(str(file),encoding='utf-8') as csvfile:
csvreader = csv.reader(csvfile)
next(csvreader,None)
for row in csvreader:
try:
if not Influencer.objects.filter(username = row[1]).exists() and check_email(row[2]):
_,created = Influencer.objects.get_or_create(
full_name = row[0],
username = row[1],
email_id = clean_email(row[2]),
external_url = row[8],
)
except Exception as e:
print(e)
How should I write the code so that I can insert the categories in the many to many field with the respective Primary key of influencer.
Are there any other alternatives other than using a ManyToManyField? I have tried django-multiselected field but it doesn't work well.
回答1:
This should do the trick:
import ast
def write_to_db(file):
with open(str(file),encoding='utf-8') as csvfile:
csvreader = csv.reader(csvfile)
next(csvreader,None)
for row in csvreader:
try:
if not check_email(row[2]):
continue
influencer, _ = Influencer.objects.get_or_create(
full_name = row[0],
username = row[1],
email_id = clean_email(row[2]),
external_url = row[8],
)
categories_str = row[5]
category_names = ast.literal_eval(categories_str)
category_names = map(str.lower, category_names) # normalize them, all lowercase
category_names = list(set(category_names)) # remove duplicates
for category_name in category_names:
category, _ = Category.objects.get_or_create(name=category_name)
influencer.categories.add(category)
except Exception as e:
print(e)
I'm assuming the format I see there in the categories column is consistent with the snippet you pasted (using single quotes and []
to denote a list). In that case the ast
module can be used to parse this column directly into a python literal, a list of strings ;)
来源:https://stackoverflow.com/questions/56768792/how-to-populate-a-manytomany-field-in-django-using-a-csv-file