尝试使用 Faker 填充 Django 应用程序数据库

Trying to populate Django app database with Faker

我正在尝试用 Faker 填充 Django 数据库。 我创建了一个 Django 项目(名称 = first_project),然后我创建了一个 Django 应用程序(名称 = first_app)。 我在我的应用程序中创建了模型,然后在我的 first_project 主文件夹(包含 manage.py 的文件夹)中创建了一个名为 'populate_first_app.py' 的文件,并填写了以下代码:

import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE','first_project.settings')
 
import django
django.setup()
 
import random
from first_app.models import Topic,Webpage,AccessRecord
from faker import Faker
 
fakegen = Faker()
topics = ['Search','Social','Marketplace','News','Games']
 
def add_topic():
    t = Topic.objects.get_or_create(top_name=random.choice(topics))[0]
    t.save()
    return t
 
def populate(N=5):
 
    for entry in range(N):
 
        top = add_topic()
        fake_url = fakegen.url()
        fake_date = fakegen.date()
        fake_name = fakegen.company()
 
        webpg = Webpage.objects.get_or_create(topic=top,url=fake_url,name=fake_name)[0]
 
        acc_rec = AccessRecord.objects.get_or_create(name=webpg,date=fake_date)[0]
 
if __name__ == '__main__':
    print("populating script!")
    populate(20)
    print("populating complete!")

进行迁移和 运行 代码后,我的终端显示:

(MyDjangoEnv) E:\Django\first_project>python populate_first_app.py
populating script!
Traceback (most recent call last):
  File "populate_first_app.py", line 34, in <module>
    populate(20)
  File "populate_first_app.py", line 28, in populate
    webpg = Webpage.objects.get_or_create(topic=top,url=fake_url,name=fake_name)[0]
  File "C:\Users\vedan\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\vedan\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\query.py", line 573, in get_or_create
    return self.get(**kwargs), False
  File "C:\Users\vedan\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\query.py", line 418, in get
    clone = self._chain() if self.query.combinator else self.filter(*args, **kwargs)
  File "C:\Users\vedan\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\query.py", line 942, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "C:\Users\vedan\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\query.py", line 962, in _filter_or_exclude
    clone._filter_or_exclude_inplace(negate, *args, **kwargs)
  File "C:\Users\vedan\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\query.py", line 969, in _filter_or_exclude_inplace
    self._query.add_q(Q(*args, **kwargs))
  File "C:\Users\vedan\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\sql\query.py", line 1358, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "C:\Users\vedan\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\sql\query.py", line 1377, in _add_q
    child_clause, needed_inner = self.build_filter(
  File "C:\Users\vedan\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\sql\query.py", line 1258, in build_filter
    lookups, parts, reffed_expression = self.solve_lookup_type(arg)
  File "C:\Users\vedan\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\sql\query    _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
  File "C:\Users\vedan\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\sql\query.py", line 1481, in names_to_path
    raise FieldError("Cannot resolve keyword '%s' into field. "
django.core.exceptions.FieldError: Cannot resolve keyword 'topic' into field. Choices are: Topic, Topic_id, accessrecord, id, name, url

有人可以帮助我吗? PS: 我是新手,这是我的第一个问题。

谢谢你:)

get_or_create() 方法中使用 Topic 而不是 topic

webpg = Webpage.objects.get_or_create(Topic=top,url=fake_url,name=fake_name)[0]