import factory

from supplements.models import Ingredient, Measurement, IngredientComposition, Supplement

DEFAULT_INGREDIENT_NAME = 'Leucine'
DEFAULT_INGREDIENT_HL_MINUTE = 50

DEFAULT_MEASUREMENT_NAME = 'milligram'
DEFAULT_MEASUREMENT_SHORT_NAME = 'mg'

DEFAULT_SUPPLEMENT_NAME = 'BCAA'


class IngredientFactory(factory.Factory):
    class Meta:
        model = Ingredient

    name = DEFAULT_INGREDIENT_NAME
    half_life_minutes = DEFAULT_INGREDIENT_HL_MINUTE


class MeasurementFactory(factory.Factory):
    class Meta:
        model = Measurement

    name = DEFAULT_MEASUREMENT_NAME


class IngredientCompositionFactory(factory.Factory):
    class Meta:
        model = IngredientComposition

    ingredient = factory.SubFactory(IngredientFactory)
    measurement_unit = factory.SubFactory(MeasurementFactory)


class SupplementFactory(factory.Factory):
    class Meta:
        model = Supplement

    name = DEFAULT_SUPPLEMENT_NAME

    @factory.post_generation
    def ingredient_composition(self, create, extracted, **kwargs):
        if not create:
            # Simple build, do nothing.
            return

        if extracted:
            # A list of groups were passed in, use them
            for group in extracted:
                print (group)
                self.ingredient_composition.add(group)

import factory

from supplements.models import Ingredient, Measurement, IngredientComposition, Supplement

DEFAULT_INGREDIENT_NAME = 'Leucine'
DEFAULT_INGREDIENT_HL_MINUTE = 50

DEFAULT_MEASUREMENT_NAME = 'milligram'
DEFAULT_MEASUREMENT_SHORT_NAME = 'mg'

DEFAULT_SUPPLEMENT_NAME = 'BCAA'


class IngredientFactory(factory.DjangoModelFactory):
    class Meta:
        model = Ingredient

    name = DEFAULT_INGREDIENT_NAME
    half_life_minutes = DEFAULT_INGREDIENT_HL_MINUTE


class MeasurementFactory(factory.DjangoModelFactory):
    class Meta:
        model = Measurement

    name = DEFAULT_MEASUREMENT_NAME


class IngredientCompositionFactory(factory.DjangoModelFactory):
    class Meta:
        model = IngredientComposition

    ingredient = factory.SubFactory(IngredientFactory)
    measurement_unit = factory.SubFactory(MeasurementFactory)


class SupplementFactory(factory.DjangoModelFactory):
    class Meta:
        model = Supplement

    name = DEFAULT_SUPPLEMENT_NAME

    @factory.post_generation
    def ingredient_composition(self, create, extracted, **kwargs):
        if not create:
            # Simple build, do nothing.
            return

        if extracted:
            # A list of groups were passed in, use them
            for group in extracted:
                self.ingredient_composition.add(group)

Swap out native factory.Factory with Django specific factory .... now all factory() calls actually save versus ... before nasty assumption
Swap out native factory.Factory with Django specific factory .... now all factory() calls actually save versus ... before nasty assumption

  import factory
  
  from supplements.models import Ingredient, Measurement, IngredientComposition, Supplement
  
  DEFAULT_INGREDIENT_NAME = 'Leucine'
  DEFAULT_INGREDIENT_HL_MINUTE = 50
  
  DEFAULT_MEASUREMENT_NAME = 'milligram'
  DEFAULT_MEASUREMENT_SHORT_NAME = 'mg'
  
  DEFAULT_SUPPLEMENT_NAME = 'BCAA'
  
  
- class IngredientFactory(factory.Factory):
+ class IngredientFactory(factory.DjangoModelFactory):
      class Meta:
          model = Ingredient
  
      name = DEFAULT_INGREDIENT_NAME
      half_life_minutes = DEFAULT_INGREDIENT_HL_MINUTE
  
  
- class MeasurementFactory(factory.Factory):
+ class MeasurementFactory(factory.DjangoModelFactory):
      class Meta:
          model = Measurement
  
      name = DEFAULT_MEASUREMENT_NAME
  
  
- class IngredientCompositionFactory(factory.Factory):
+ class IngredientCompositionFactory(factory.DjangoModelFactory):
      class Meta:
          model = IngredientComposition
  
      ingredient = factory.SubFactory(IngredientFactory)
      measurement_unit = factory.SubFactory(MeasurementFactory)
  
  
- class SupplementFactory(factory.Factory):
+ class SupplementFactory(factory.DjangoModelFactory):
      class Meta:
          model = Supplement
  
      name = DEFAULT_SUPPLEMENT_NAME
  
      @factory.post_generation
      def ingredient_composition(self, create, extracted, **kwargs):
          if not create:
              # Simple build, do nothing.
              return
  
          if extracted:
              # A list of groups were passed in, use them
              for group in extracted:
-                 print (group)
                  self.ingredient_composition.add(group)
  
Swap out native factory.Factory with Django specific factory .... now all factory() calls actually save versus ... before nasty assumption
## Code Before:
import factory

from supplements.models import Ingredient, Measurement, IngredientComposition, Supplement

DEFAULT_INGREDIENT_NAME = 'Leucine'
DEFAULT_INGREDIENT_HL_MINUTE = 50

DEFAULT_MEASUREMENT_NAME = 'milligram'
DEFAULT_MEASUREMENT_SHORT_NAME = 'mg'

DEFAULT_SUPPLEMENT_NAME = 'BCAA'


class IngredientFactory(factory.Factory):
    class Meta:
        model = Ingredient

    name = DEFAULT_INGREDIENT_NAME
    half_life_minutes = DEFAULT_INGREDIENT_HL_MINUTE


class MeasurementFactory(factory.Factory):
    class Meta:
        model = Measurement

    name = DEFAULT_MEASUREMENT_NAME


class IngredientCompositionFactory(factory.Factory):
    class Meta:
        model = IngredientComposition

    ingredient = factory.SubFactory(IngredientFactory)
    measurement_unit = factory.SubFactory(MeasurementFactory)


class SupplementFactory(factory.Factory):
    class Meta:
        model = Supplement

    name = DEFAULT_SUPPLEMENT_NAME

    @factory.post_generation
    def ingredient_composition(self, create, extracted, **kwargs):
        if not create:
            # Simple build, do nothing.
            return

        if extracted:
            # A list of groups were passed in, use them
            for group in extracted:
                print (group)
                self.ingredient_composition.add(group)

## Instruction:
Swap out native factory.Factory with Django specific factory .... now all factory() calls actually save versus ... before nasty assumption
## Code After:
import factory

from supplements.models import Ingredient, Measurement, IngredientComposition, Supplement

DEFAULT_INGREDIENT_NAME = 'Leucine'
DEFAULT_INGREDIENT_HL_MINUTE = 50

DEFAULT_MEASUREMENT_NAME = 'milligram'
DEFAULT_MEASUREMENT_SHORT_NAME = 'mg'

DEFAULT_SUPPLEMENT_NAME = 'BCAA'


class IngredientFactory(factory.DjangoModelFactory):
    class Meta:
        model = Ingredient

    name = DEFAULT_INGREDIENT_NAME
    half_life_minutes = DEFAULT_INGREDIENT_HL_MINUTE


class MeasurementFactory(factory.DjangoModelFactory):
    class Meta:
        model = Measurement

    name = DEFAULT_MEASUREMENT_NAME


class IngredientCompositionFactory(factory.DjangoModelFactory):
    class Meta:
        model = IngredientComposition

    ingredient = factory.SubFactory(IngredientFactory)
    measurement_unit = factory.SubFactory(MeasurementFactory)


class SupplementFactory(factory.DjangoModelFactory):
    class Meta:
        model = Supplement

    name = DEFAULT_SUPPLEMENT_NAME

    @factory.post_generation
    def ingredient_composition(self, create, extracted, **kwargs):
        if not create:
            # Simple build, do nothing.
            return

        if extracted:
            # A list of groups were passed in, use them
            for group in extracted:
                self.ingredient_composition.add(group)
