Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix process_field so that it works for m2m fields in Django 2.0 #107

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions autofixture/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import warnings
from django import VERSION
from django.db.models import fields, ImageField
from django.conf import settings
from django.db.models.fields import related
Expand Down Expand Up @@ -403,11 +404,14 @@ def get_value(self, field):
value = generator()
return value

def process_field(self, instance, field):
def process_field(self, instance, field, is_m2m=False):
value = self.get_value(field)
if value is self.IGNORE_FIELD:
return
setattr(instance, field.name, value)
if is_m2m and VERSION[0] >= 2:
getattr(instance, field.name).set(value)
else:
setattr(instance, field.name, value)

def process_m2m(self, instance, field):
# check django's version number to determine how intermediary models
Expand All @@ -417,7 +421,7 @@ def process_m2m(self, instance, field):
auto_created_through_model = through._meta.auto_created

if auto_created_through_model:
return self.process_field(instance, field)
return self.process_field(instance, field, is_m2m=True)
# if m2m relation has intermediary model:
# * only generate relation if 'generate_m2m' is given
# * first generate intermediary model and assign a newly created
Expand Down