'populate Django database my code with Faker library, when I was compiling it did not showing any error, but not populate fake data in admin page

This is my first file which is the script for populating the database: ('fake_data.py') when I run python fake_data.py it showing "populating fake_data populating complated!" enter image description here

This file is 'module.py enter image description here

But when I am running server the fake data is not in admin page. enter image description here



Solution 1:[1]

Note

I don't understand why are you using faker but if you want to populate your database with random data then in

fake_data.py

# Import your libraries here
import random

# define your here
def main():
    # Call populate
    print("Populating random data begins ...")
    populate(20)
    print("Populating random data ends ...")


# random tasks
todos = ["foo", "bar", "bazz"]


def add_todo():
    # Create and save records in database
    q = TODO.objects.create(name=random.choice(todos))
    return q


def populate(N=5):
    for counter in range(N):
        entry = add_todo()

# call your main
if __name__ == "__main__":
    main()
  • Note that if you don't use q and entry else where in your code, then remove both of them.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 shah sawood