So I've been following this guide from Django 1.1 but I'm actually using Django 2 for how to pre-populate Django database I'm using SQLite database and this is my code with Faker library but it just won't run when I want to run it in the CMD.
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'first_project.settings')
import django
django.setup()
## FAKE POPULATION SCRIPT:
import random
from first_app.models import AccessRecord,Webpage,Topic
from faker import Faker
# Creating a fake generator:
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):
# GET THE TOPIC FOR THE ENTRY:
top = add_topic()
# Create the fake data for that entry:
fake_url = fakegen.url()
fake_date = fakegen.date()
fake_name = fakegen.company()
# Create the new webpage entry:
webpg = Webpage.objects.get_or_create(topic = top, url = fake_url, name = fake_name)[0]
# Create a fake access record for that webpage
acc_rec = AccessRecord.get_or_create(name = webpg, date = fake_date)[0]
if __name__ == '__main__':
print("Populating Script!")
populate(20)
print("Populating Complete!")
This is the models.py
file of the only app I have in this project.
from django.db import models
# Create your models here.
class Topic(models.Model):
top_name = models.CharField(max_length=264, unique=True)
def __str__(self):
return self.top_name
class Webpage(models.Model):
topic = models.ForeignKey(Topic,on_delete=models.CASCADE)
name = models.CharField(max_length=264,unique=True)
url = models.URLField(unique=True)
def __str__(self):
return self.name
class AccessRecord(models.Model):
name = models.ForeignKey(Webpage,on_delete=models.CASCADE)
date = models.DateField
def __str__(self):
return str(self.date)
And this is the error that I am getting
Populating script!
Traceback (most recent call last):
File "populate_first_app.py", line 41, in <module>
populate(20)
File "populate_first_app.py", line 37, in populate
acc_rec = AccessRecord.objects.get_or_create(name=webpg,date=fake_date)[0]
File "C:\Users\divya\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\m
odels\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\divya\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\m
odels\query.py", line 559, in get_or_create
File "C:\Users\divya\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\sql\query.py", line 1250, in build_filter
lookups, parts, reffed_expression = self.solve_lookup_type(arg)
File "C:\Users\divya\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\sql\query.py", line 1087, in solve_lookup_type
_, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
File "C:\Users\divya\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\models\sql\query.py", line 1482, in names_to_path
raise FieldError("Cannot resolve keyword '%s' into field. "
django.core.exceptions.FieldError: Cannot resolve keyword 'date' into field. Choices are: id, name, name_id
Your error on the line: date = models.DateField
. Correct on date = models.DateField()
and make migrations.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.