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

Don't set null on M2M fields #28

Merged
merged 3 commits into from
Jan 8, 2016
Merged
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
6 changes: 5 additions & 1 deletion typedmodels/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ class Meta:
declared_fields = dict((name, element) for name, element in classdict.items() if isinstance(element, Field))

for field_name, field in declared_fields.items():
field.null = True
# In Django 1.8+, warnings will be triggered by the system
# check for M2M fields setting if we set null to True. Prevent
# those warnings by setting null only for non-M2M fields.
if django.VERSION < (1, 8) or not field.many_to_many:
field.null = True

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting many to many fields null had no effect before Django 1.8, correct? Maybe just make this if not field.many_to_many:

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting many to many fields null had no effect before Django 1.8, correct?

That is correct and this PR leaves the Django < 1.8 behavior the same. The reason for the Django version test is that many_to_many is an attribute that was added to django.db.models.fields.Field in Django 1.8. I'm hoping Django 1.7 is not long for this world so hopefully this can merge as is and then the Django version conditional can be removed when 1.7 support is dropped.

if isinstance(field, models.fields.related.RelatedField):
# Monkey patching field instance to make do_related_class use created class instead of base_class.
# Actually that class doesn't exist yet, so we just monkey patch base_class for a while,
Expand Down