Stack Overflow Asked by rmoore35 on December 4, 2021
I am trying to use factory boy and faker to generate some fake data for a website I am building. Here is my models.py:
# External Imports
from django.db import models
import uuid
# Internal Imports
from applications.models.application import Application
from users.models.user import User
from .session import Session
# Fake data
import factory
import factory.django
import factory.fuzzy
from datetime import datetime
from faker import Faker
from faker.providers import BaseProvider
import random
class ButtonClick(models.Model):
"""**Database model that tracks and saves button clicks for an application**
"""
# identifier
id = models.UUIDField(default=uuid.uuid4, primary_key=True, editable=False)
# info
button_name = models.CharField(max_length=128, null=True, blank=True)
application = models.ForeignKey(
Application, related_name='button_clicks', null=True, blank=True, on_delete=models.CASCADE)
user = models.ForeignKey(
User, related_name='button_clicks', null=True, blank=True, on_delete=models.CASCADE)
session = models.ForeignKey(
Session, related_name='button_clicks', null=True, blank=True, on_delete=models.CASCADE)
timestamp = models.DateTimeField(auto_now=True)
class Meta:
db_table = 'button_clicks'
ordering = ('-timestamp', )
def __str__(self):
return f'{self.application} - {self.button_name}'
fake = Faker()
faker = Factory.create()
class ApplicationFactory(factory.DjangoModelFactory):
class Meta:
model = Application
application = factory.LazyAttribute(lambda _: faker.word())
class FakeButtonClick(factory.django.DjangoModelFactory):
class Meta:
model = ButtonClick
button_name = factory.Faker('first_name')
application = factory.SubFactory(ApplicationFactory)
user = factory.Faker('name')
session = factory.Faker('random_int')
timestamp = factory.Faker('date')
When I try to run the following code in the terminal, I get an error:
>>> from analytics.models.button_click import FakeButtonClick
>>> for _ in range(200): FakeButtonClick.create()
...
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/ryan/bloks/bloks-backend/venv/lib/python3.7/site-packages/factory/base.py", line 564, in create
return cls._generate(enums.CREATE_STRATEGY, kwargs)
File "/Users/ryan/bloks/bloks-backend/venv/lib/python3.7/site-packages/factory/django.py", line 141, in _generate
return super(DjangoModelFactory, cls)._generate(strategy, params)
File "/Users/ryan/bloks/bloks-backend/venv/lib/python3.7/site-packages/factory/base.py", line 501, in _generate
return step.build()
File "/Users/ryan/bloks/bloks-backend/venv/lib/python3.7/site-packages/factory/builder.py", line 279, in build
kwargs=kwargs,
File "/Users/ryan/bloks/bloks-backend/venv/lib/python3.7/site-packages/factory/base.py", line 315, in instantiate
return self.factory._create(model, *args, **kwargs)
File "/Users/ryan/bloks/bloks-backend/venv/lib/python3.7/site-packages/factory/django.py", line 185, in _create
return manager.create(*args, **kwargs)
File "/Users/ryan/bloks/bloks-backend/venv/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/ryan/bloks/bloks-backend/venv/lib/python3.7/site-packages/django/db/models/query.py", line 431, in create
obj = self.model(**kwargs)
File "/Users/ryan/bloks/bloks-backend/venv/lib/python3.7/site-packages/django/db/models/base.py", line 482, in __init__
_setattr(self, field.name, rel_obj)
File "/Users/ryan/bloks/bloks-backend/venv/lib/python3.7/site-packages/django/db/models/fields/related_descriptors.py", line 219, in __set__
self.field.remote_field.model._meta.object_name,
ValueError: Cannot assign "9714": "ButtonClick.application" must be a "Application" instance.
I have created some very simple data using factory boy and faker in the past but the traceback seems to be implying that I need to create an application instance within my FakeButtonClick class? I checked the documentation and application doesn’t appear to be an available instance for factory boy/faker. Do I need to create the instance myself? Maybe a subfactory?
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:
Factory
class for each of these models.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()
.
Answered by Xelnor on December 4, 2021
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')
Answered by crimsonpython24 on December 4, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP