How can I create a field in Factory-Boy to TaggableManager from django-taggit?
The following does not work:
First:
class ExperimentFactory(factory.DjangoModelFactory):
class Meta:
model = Experiment
django_get_or_create = ('owner', 'name', 'start_date', 'stop_date', )
owner = factory.SubFactory(UserFactory)
name = factory.Sequence(lambda n : 'Experiment %s' % n)
start_date = fuzzy.FuzzyDate(start_date=datetime.date(2003, 2, 1),
end_date = datetime.date.today() + timedelta(days=4*52*7+5))
tags = [u'abc, cde', u'xzy']
The error is: TypeError: 'tags' is an invalid keyword argument for this function
Second: (with factory.post_generation
). So Instead of the last line there is
@factory.post_generation
def post_tags(self, create, extracted, **kwargs):
self.tags = [u'abc, cde', u'xzy']
# self.save() # This does not have any effects
I get as result by Experiment.objects.get(pk=1).tags
is []
For some details:
The testing of the objects in a TestCase
is:
experiment = ExperimentFactory(owner = self.user)
saved_experiment = Experiment.objects.get(name=experiment.name)
self.assertEquals(len(experiment.tags), len(saved_experiment.tags.all())) # AssertError: 2 != 0
self.assertItemsEqual(experiment.tags, saved_experiment.tags.all())
The odd thing is that in self.experiments
the tags are set. The post_tags()
method is called.
The model is:
class Experiment(models.Model):
owner = models.ForeignKey(User, related_name='experiments')
name = models.CharField(max_length=32, default='')
start_date = models.DateField()
tags = TaggableManager()
finally found the solution: