regarding adding data into database using faker library

28 views
Skip to first unread message

Sahil Sharma

unread,
Sep 25, 2019, 7:04:10 AM9/25/19
to Django users
So I was adding data into the user model that is create using faker library  but i am facing certain error

 my populatescript screenshot is inserted

2019-09-24-1569346732_screenshot_1920x1080.jpg

 
here is my user model class

from django.db import models

# Create your models here.

class user(models.Model):
firstname=models.CharField(max_length=250,unique=False)
lastname=models.CharField(max_length=250,unique=False)
emailid=models.CharField(max_length=500,unique=True)
def __str__(self):

return self.emailid





Cornelis Poppema

unread,
Sep 25, 2019, 9:13:00 AM9/25/19
to Django users
You almost had it! You're already setting DJANGO_SETTINGS_MODULE before you call django.setup() inside your main function. However, you also call django.setup() on line 4. You need to set DJANGO_SETTINGS_MODULE before the first django.setup().

Sahil Sharma

unread,
Sep 26, 2019, 8:23:33 AM9/26/19
to Django users

2019-09-26-1569500258_screenshot_1920x1080.jpg

After fixing the error of django.setup() I ran the file but it is not working and also showing the error. I am sharing the screenshot of the program along with the error it is showing

Cornelis Poppema

unread,
Sep 26, 2019, 8:42:05 AM9/26/19
to Django users
The location of your first django.setup() on line 4 was actually what you need. Right now it does show the same error but from a different line: the line where you import your model.

- you need to do django.setup() before importing any of your project code
- you need to set DJANGO_SETTINGS_MODULE before calilng django.setup()

so your file should like this:

#!/usr/bin/env python
import os
os
.environ.setdefault('DJANGO_SETTINGS_MODULE', 'firstproject.setting')


import django
django
.setup()


import random


from faker import Faker


from firstapp.models import user


def populate(N):
   
...




if __name__ == '__main__':
    populate
(10)

Sahil Sharma

unread,
Sep 26, 2019, 10:26:18 AM9/26/19
to Django users

2019-09-26-1569507899_screenshot_1920x1080.jpg

I did the changes but it not working. I am sharing the screenshot of the program
On Wednesday, 25 September 2019 16:34:10 UTC+5:30, Sahil Sharma wrote:

Cornelis Poppema

unread,
Sep 26, 2019, 10:30:20 AM9/26/19
to Django users
It seems you have an unfortunate typo in your environment variable name.

Change DJANGO_SETTING_MODULE -> DJANGO_SETTINGS_MODULE and try again.
Reply all
Reply to author
Forward
0 new messages