9/23/2023 0 Comments Faker in python![]() ![]() Define a Factory class for each of these models.With FactoryBoy, this means that you'll have to: 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). Your ButtonClick model has 3 fields defined as a ForeignKey: application, user and session. Do I need to create the instance myself? Maybe a subfactory? 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. ValueError: Cannot assign "9714": "ButtonClick.application" must be a "Application" instance. Return getattr(self.get_queryset(), name)(*args, **kwargs)įile "/Users/ryan/bloks/bloks-backend/venv/lib/python3.7/site-packages/django/db/models/query.py", line 431, in createįile "/Users/ryan/bloks/bloks-backend/venv/lib/python3.7/site-packages/django/db/models/base.py", line 482, in _init_įile "/Users/ryan/bloks/bloks-backend/venv/lib/python3.7/site-packages/django/db/models/fields/related_descriptors.py", line 219, in _set_ Return self.factory._create(model, *args, **kwargs)įile "/Users/ryan/bloks/bloks-backend/venv/lib/python3.7/site-packages/factory/django.py", line 185, in _createįile "/Users/ryan/bloks/bloks-backend/venv/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method Return super(DjangoModelFactory, cls)._generate(strategy, params)įile "/Users/ryan/bloks/bloks-backend/venv/lib/python3.7/site-packages/factory/base.py", line 501, in _generateįile "/Users/ryan/bloks/bloks-backend/venv/lib/python3.7/site-packages/factory/builder.py", line 279, in buildįile "/Users/ryan/bloks/bloks-backend/venv/lib/python3.7/site-packages/factory/base.py", line 315, in instantiate Return cls._generate(enums.CREATE_STRATEGY, kwargs)įile "/Users/ryan/bloks/bloks-backend/venv/lib/python3.7/site-packages/factory/django.py", line 141, in _generate > for _ in range(200): FakeButtonClick.create()įile "/Users/ryan/bloks/bloks-backend/venv/lib/python3.7/site-packages/factory/base.py", line 564, in create When I try to run the following code in the terminal, I get an error: > from _click import FakeButtonClick Return f''Ĭlass ApplicationFactory(factory.DjangoModelFactory):Īpplication = factory.LazyAttribute(lambda _: faker.word())Ĭlass FakeButtonClick():īutton_name = factory.Faker('first_name')Īpplication = factory.SubFactory(ApplicationFactory) Timestamp = models.DateTimeField(auto_now=True) Session, related_name='button_clicks', null=True, blank=True, on_delete=models.CASCADE) User, related_name='button_clicks', null=True, blank=True, on_delete=models.CASCADE) Id = models.UUIDField(default=uuid.uuid4, primary_key=True, editable=False)īutton_name = models.CharField(max_length=128, null=True, blank=True)Īpplication, related_name='button_clicks', null=True, blank=True, on_delete=models.CASCADE) """**Database model that tracks and saves button clicks for an application** Here is my models.py: # External Importsįrom import Application It does strong validation and produces correct results.I am trying to use factory boy and faker to generate some fake data for a website I am building. ![]() But for my purposes this function is all I need. PS: The subprocess call could probably be replaced with ctypes directly calling Windows kernel/DLLs. Uses Windows API to get the computer's permanent UUID, then processes the string to ensure it's a valid UUID, and lastly returns a Python object ( ) which gives you convenient ways to use the data (such as 128-bit integer, hex string, etc). ![]() # Ensure we have exactly 32 characters (16 bytes). # and useless dashes etc, by only keeping hex (0-9 A-F) chars. # Remove the surrounding whitespace (newlines, space, etc) # Attempt to extract the UUID from the command's result. Txt = subprocess.check_output("wmic csproduct get uuid").decode() # Ask Windows for the device's permanent UUID. from typing import Optionalĭef get_windows_uuid() -> Optional: If you want a permanent UUID that identifies a machine uniquely on Windows, you can use this trick: (Copied from my answer at ). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |