i'm trying to import users in database using bulk create but i'm getting exception ValueError: Field 'id' expected a number but got nan.
I'm getting exception when bulk create profiles.
def import_users(csv_file):
df = pd.read_csv(io.StringIO(csv_file.decode('utf-8')))
users = []
profiles = []
for index, row in df.iterrows():
user = User()
user.username = row['username']
user.password = make_password(str(row['password']))
user.first_name = row['firstname']
user.last_name = row['lastname']
user.is_active = True
user.email = row.get('email', None)
user.profile = Profile()
user.profile.app_id = row['app_id']
user.profile.company_id = row['company_id']
user.profile.user = user
users.append(user)
profiles.append(user.profile)
try:
users = User.objects.bulk_create(users, ignore_conflicts=False)
except Exception as e:
LOGGER.exception(str(e))
try:
Profile.objects.bulk_create(profiles, ignore_conflicts=False)
except Exception as e:
LOGGER.exception(str(e))
Model for Profile is:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
company = models.ForeignKey(to='Company', on_delete=models.SET_NULL, null=True)
app = models.ForeignKey(to='App', default=1000, on_delete=models.SET_DEFAULT, null=False)
I checked this solution on SO, but no help for me.