You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When a post_generation hook wraps a field name that is also overriden by a Trait, and both are called on .create(), then none of them appear to have their desired effect.
To Reproduce
Should happen just by running the provided code.
Model / Factory code
# -------------------------- models.py --------------------------classCategory(models.Model):
name=models.CharField(max_length=255, unique=True)
slug=models.SlugField(max_length=255, unique=True)
available=models.BooleanField(default=True)
classMeta:
verbose_name='category'verbose_name_plural='categories'indexes= [models.Index(fields=['name'])]
def__str__(self):
returnself.nameclassProduct(models.Model):
categories=models.ManyToManyField(
Category,
through='ProductInCategory',
related_name='products'
)
name=models.CharField(max_length=150)
slug=models.SlugField(max_length=255)
description=models.TextField(blank=True)
image=models.ImageField(upload_to='images/products/')
price=models.DecimalField(max_digits=7, decimal_places=2)
is_active=models.BooleanField(default=True)
created_at=models.DateTimeField(auto_now_add=True)
updated_at=models.DateTimeField(auto_now=True)
classMeta:
ordering= ('-created_at',)
indexes= [
models.Index(fields=['name']),
]
def__str__(self):
returnf'{self.name}'classProductInCategory(models.Model):
''' Intermediate model for many-to-many relationship between Category and Product. '''category=models.ForeignKey(Category, null=True, on_delete=models.SET_NULL)
product=models.ForeignKey(Product, null=True, on_delete=models.SET_NULL)
classMeta:
unique_together= ('category', 'product')
# -------------------------- factories.py --------------------------classCategoryFactory(DjangoModelFactory):
''' Category model factory. Allows associated models at build time through the following keywords: - Product: - `products:` as an override. - `with_products:` trait for automatic creation. '''classMeta:
model='shop.Category'django_get_or_create= ('name',)
classParams:
with_products=factory.Trait(
products=factory.RelatedFactoryList(
'apps.shop.tests_v2.factories.ProductInCategoryFactory',
'category',
size=lambda: random.randint(1, 3),
))
name=factory.Sequence(lambdan: f'Category {n}')
slug=factory.LazyAttribute(lambdao: slugify(o.name))
available=True@factory.post_generationdefproducts(self, create, extracted, **kwargs):
''' Catch `products` keyword override at build/creation time. '''ifnot (createandextracted):
returnself.products.add(*extracted)
classProductFactory(DjangoModelFactory):
''' Product model factory. '''classMeta:
model='shop.Product'django_get_or_create= ('name',)
name=factory.Sequence(lambdan: f'Product {n}')
slug=factory.LazyAttribute(lambdao: slugify(o.name))
description=factory.Faker('text')
image=factory.django.ImageField()
price=factory.Faker('pydecimal', left_digits=2, right_digits=2, positive=True)
is_active=TrueclassProductInCategoryFactory(DjangoModelFactory):
''' Product <--> Category relationship intermediate model factory. '''classMeta:
model='shop.ProductInCategory'django_get_or_create= ('category', 'product')
category=factory.SubFactory(CategoryFactory)
product=factory.SubFactory(ProductFactory)
The issue
When used in isolation, CategoryFactory.create(products=[...]) or CategoryFactory.create(with_products=True) work as expected, that is to say: the first one uses the provided products list and sets them to the Category model object, and the second one creates new products for the Category model object. But when used together as CategoryFactory.create(with_products=True, products=[...]) then the resulting category object has no related products at all. I would understand if one were to override the other and the result was only one of the previous examples, but this seemed like a bug. Am I doing something wrong?
Edit 1: Apologies, I forgot to add the Category <-> Product intermediate model from models.py, it's there now.
Edit 2: Please note, I don't think it's because of where the M2M field is placed, since I tried it the other way around (setting the post_generation hook and the Trait on ProductFactory) and it happened that way as well.
The text was updated successfully, but these errors were encountered:
Description
When a post_generation hook wraps a field name that is also overriden by a Trait, and both are called on .create(), then none of them appear to have their desired effect.
To Reproduce
Should happen just by running the provided code.
Model / Factory code
The issue
When used in isolation,
CategoryFactory.create(products=[...])
orCategoryFactory.create(with_products=True)
work as expected, that is to say: the first one uses the provided products list and sets them to the Category model object, and the second one creates new products for the Category model object. But when used together asCategoryFactory.create(with_products=True, products=[...])
then the resulting category object has no related products at all. I would understand if one were to override the other and the result was only one of the previous examples, but this seemed like a bug. Am I doing something wrong?Edit 1: Apologies, I forgot to add the Category <-> Product intermediate model from models.py, it's there now.
Edit 2: Please note, I don't think it's because of where the M2M field is placed, since I tried it the other way around (setting the post_generation hook and the Trait on ProductFactory) and it happened that way as well.
The text was updated successfully, but these errors were encountered: