我目前正在使用Python 3.7.4和Django 2.2.5,但是我面临以下问题,并且我没有任何线索:
“主题”类没有“对象”成员pylint(无成员)
与“网页”和“ AccessRecord”相同。还有...
“ Generator”的实例没有“ url”成员pylint(无成员)
与“日期”和“公司”相同...
文件: first_project / populate_first_app.py (完整)
import os,sys
os.environ.setdefault('DJANGO_SETTINGS_MODULES','first-project.settings')
import django
django.setup()
# FAKE POP SCRIPT
import random
from first_app.models import AccessRecord,Topic,Webpage
from faker import Faker
fakegen = Faker()
topics = ['Search','Marketplace','Social','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 an 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.objects.get_or_create(name = webpg, date = fake_date)[0]
if __name__ == '__main__':
print("Populating script!")
populate(20)
print("populating complete!")
我写的代码 文件: first_app / models.py
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.PROTECT)
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.PROTECT)
date = models.DateField()
def __str__(self):
return str(self.date)
我尝试按照在线一些解决方案中的说明添加Pylint Args,但这只会使事情变得更加复杂。因此,我只是在VSCode用户设置>扩展名> Python中禁用了Pylint,然后检查我的脚本,我运行了python populate_first_app.py
...只是为了确保一切正常,但会弹出此错误。
django.core.exceptions.ImproperlyConfigured:请求的设置 INSTALLED_APPS,但未配置设置。你必须 定义环境变量DJANGO_SETTINGS_MODULE或调用 settings.configure(),然后再访问设置。
我不确定我要去哪里。我只是想使用Faker填充数据... 我们将不胜感激,请逐步详细介绍它,因为我只是一个初学者。