4👍
Your ButtonClick
model has 3 fields defined as a ForeignKey
: application
, user
and session
.
When you want to create a ButtonClick
instance, Django requires that you provide a valid value to each field defined as a ForeignKey — here, this means providing either model instances or None
(since those ForeignKey are nullable).
With FactoryBoy, this means that you’ll have to:
- Define a
Factory
class for each of these models. - Use a
factory.SubFactory
pointing to those factories for each of the fields.
An example would be:
class UserFactory(factory.django.DjangoModelFactory):
class Meta:
model = User
username = factory.Faker('username')
class SessionFactory(factory.django.DjangoModelFactory):
class Meta:
model = Session
uuid = factory.Faker('uuid4')
user = factory.SubFactory(UserFactory)
class ApplicationFactory(factory.django.DjangoModelFactory):
class Meta:
model = Application
name = factory.Faker('name')
class ButtonClickFactory(factory.django.DjangoModelFactory):
class Meta:
model = ButtonClick
user = factory.SubFactory(UserFactory)
# Ensure that click.user == click.session.user
session = factory.SubFactory(SessionFactory, user=factory.SelfAttribute('..user'))
application = factory.SubFactory(ApplicationFactory)
You can take a look at the docs.
By the way, with FactoryBoy’s faker integration, you don’t need to import it directly: factory.Faker('uuid4')
is equivalent to faker.Faker().uuid4()
.
1👍
Here’s my database-populate file, probably may help you (a lot simpler than your file I believe):
# Don't change the format. Order matters!
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
import django
django.setup()
import random
from faker import Faker
from todo.models import Todo
fakegen = Faker()
def populate(N = 10):
for entry in range(N):
fake_tmp = fakegen.catch_phrase()
levels = ['important', 'normal', 'unimportant']
fake_title = fake_tmp if len(fake_tmp) <= 40 else (fake_tmp[:37] + '...')
fake_desc = fakegen.sentence(nb_words=70)
fake_level = levels[random.randint(0, 2)]
todo_item = Todo.objects.get_or_create(title=fake_title, desc=fake_desc, level=fake_level)
if __name__ == '__main__':
print('Populating data...')
populate(20)
print('Populating complete')
- [Django]-JQuery not found: Uncaught ReferenceError: $ is not defined
- [Django]-Django: Safely validating untrusted HTML input
- [Django]-Django Model returning NoneType
0👍
for future readers!
To generate fake data for django
you can use django-seed
.
It’s an easy process as
-
pip install django-seed
(install django-seed) -
add
django_seed
in your apps insettings.py
file.INSTALLED_APPS = ( ... 'django_seed', )
-
python manage.py seed <app-name>
for example: to seed api app of django python manage.py seed api --number=15
If you need, you can also specify what value a particular field should have. For example, if you want to seed 15 of MyModel, but you need my_field to be the same on all of them, you can do it like this:
python manage.py seed api --number=15 --seeder "MyModel.my_field" "1.1.1.1"
- [Django]-Django. How to redirect to url with fragment identifier
- [Django]-Using matplotlib with Django Generic views
- [Django]-Resource temporarily unavailable: mod_wsgi (pid=28433): Unable to connect to WSGI daemon process
- [Django]-Django Reuseable Apps
0👍
This is how I generated fake data into Django sqlite
Mackaroo website
go to this website and fill out details and download file in any format (sql
, json
or csv
) any format
The good thing about this website you can provide regular expression
on your columns null values and any format for numbers, dates etc
then either download it or dump it in your database