![]() ![]() Use a factory.SubFactory pointing to those factories for each of the fields.Ĭlass UserFactory():Ĭlass SessionFactory():Ĭlass ApplicationFactory():Ĭlass ButtonClickFactory():.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 INSERT INTO dbo.I am trying to use factory boy and faker to generate some fake data for a website I am building. This allows us to pick precisely two "categories" for each "user": WITH rs AS Since you already have data in the Users and Categories tables, you can use a CTE with the ROW_NUMBER windowing function partitioned by UserID and ordered by an essentially random value, NEWID(). PRIMARY KEY CLUSTERED (UserID, CategoryID) Notice there is a primary key clustered index on (UserID, CategoryID) this ensures each row is unique: CREATE TABLE dbo.XREFUserCategoriesĬONSTRAINT FK_XREFUserCategories_CategoryID This is my version of the cross-reference table where you'll store users and the categories they are members of. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |