django - How to create a Factory-Boy factory for a model with TaggableManager field -


how can create field in factory-boy taggablemanager django-taggit?

the following 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' invalid keyword argument function

second: (with factory.post_generation). instead of last line there

@factory.post_generation def post_tags(self, create, extracted, **kwargs):     self.tags = [u'abc, cde', u'xzy']     # self.save() # not have effects 

i result experiment.objects.get(pk=1).tags []

for details:

the testing of objects in 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 in self.experiments tags set. post_tags() method 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 solution:

@factory.post_generation def post_tags(self, create, extracted, **kwargs):     self.tags.add(u'abc, cde', u'xzy') 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -