Я написал скрипт python, чтобы заполнить мою базу данных поддельными данными, используя библиотеку Faker для случайного проекта, пока я изучаю Django. Мой проект называется «ProTwo», и внутри этого проекта есть приложение под названием «apptwo». Это сценарий python, который я написал: (populate_users.py)
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", 'ProTwo.settings')
import django
django.setup()
from apptwo.models import User
from faker import Faker
fakegen = Faker()
def populate(N=5):
for entry in range(N):
fake_name = fakegen.name().split()
fake_first_name = fake_name[0]
fake_last_name = fake_name[1]
fake_email = fakegen.email()
user = User.objects.get_or_create(first_name = fake_first_name, last_name = fake_last_name, email = fake_email)[0]
user.save()
if __name__ == '__main__':
print('POPULATING DATABASES!')
populate(20)
print('POPULATION DONE!')
models.py:
from django.db import models
class User(models.Model):
first_name = models.CharField(max_length = 264,null = True)
last_name = models.CharField(max_length = 264, null = True)
email = models.EmailField(max_length=254, unique = True)
(views.py):
from django.shortcuts import render
from django.http import HttpResponse
from apptwo.models import User
def index(request):
return render(request,'app_two/index.html')
def users(request):
user_list = User.objects.order_by('first_name')
user_dict = {'users': user_list}
return render(request,'app_two/users.html', context = user_dict)
(urls.py):
from django.contrib import admin
from django.conf.urls import url, include
from apptwo import views
urlpatterns = [
url(r'^$',views.index, name = 'index'),
url('admin/', admin.site.urls),
url(r'^users/', include('apptwo.urls')),
]
, и это ошибка при выполнении команды 'python populate_users.py', и я выполнил все необходимые миграции:
Traceback (most recent call last):
File "populate_users.py", line 27, in <module>
populate(20)
File "populate_users.py", line 21, in populate
user = User.objects.get_or_create(first_name = fake_first_name, last_name = fake_last_name, email = fake_email)[0]
File "C:\Users\harsh\anaconda3\envs\newEnv\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\harsh\anaconda3\envs\newEnv\lib\site-packages\django\db\models\query.py", line 562, in get_or_create
return self._create_object_from_params(kwargs, params)
File "C:\Users\harsh\anaconda3\envs\newEnv\lib\site-packages\django\db\models\query.py", line 604, in _create_object_from_params
raise e
File "C:\Users\harsh\anaconda3\envs\newEnv\lib\site-packages\django\db\models\query.py", line 596, in _create_object_from_params
obj = self.create(**params)
File "C:\Users\harsh\anaconda3\envs\newEnv\lib\site-packages\django\db\models\query.py", line 433, in create
obj.save(force_insert=True, using=self.db)
File "C:\Users\harsh\anaconda3\envs\newEnv\lib\site-packages\django\db\models\base.py", line 745, in save
self.save_base(using=using, force_insert=force_insert,
File "C:\Users\harsh\anaconda3\envs\newEnv\lib\site-packages\django\db\models\base.py", line 782, in save_base
updated = self._save_table(
File "C:\Users\harsh\anaconda3\envs\newEnv\lib\site-packages\django\db\models\base.py", line 887, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "C:\Users\harsh\anaconda3\envs\newEnv\lib\site-packages\django\db\models\base.py", line 924, in _do_insert
return manager._insert(
File "C:\Users\harsh\anaconda3\envs\newEnv\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\harsh\anaconda3\envs\newEnv\lib\site-packages\django\db\models\query.py", line 1204, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "C:\Users\harsh\anaconda3\envs\newEnv\lib\site-packages\django\db\models\sql\compiler.py", line 1384, in execute_sql
cursor.execute(sql, params)
File "C:\Users\harsh\anaconda3\envs\newEnv\lib\site-packages\django\db\backends\utils.py", line 100, in execute
return super().execute(sql, params)
File "C:\Users\harsh\anaconda3\envs\newEnv\lib\site-packages\django\db\backends\utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\harsh\anaconda3\envs\newEnv\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\harsh\anaconda3\envs\newEnv\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\harsh\anaconda3\envs\newEnv\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\harsh\anaconda3\envs\newEnv\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\harsh\anaconda3\envs\newEnv\lib\site-packages\django\db\backends\sqlite3\base.py", line 396, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: UNIQUE constraint failed: apptwo_user.First_name