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 _meta warnings on django1.8 #299

Merged
merged 1 commit into from Apr 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 16 additions & 15 deletions taggit/managers.py
Expand Up @@ -26,7 +26,7 @@

from taggit.forms import TagField
from taggit.models import GenericTaggedItemBase, TaggedItem
from taggit.utils import require_instance_manager
from taggit.utils import _get_field, require_instance_manager

try:
from django.contrib.contenttypes.fields import GenericRelation
Expand Down Expand Up @@ -212,7 +212,7 @@ def similar_objects(self):
if len(lookup_keys) == 1:
# Can we do this without a second query by using a select_related()
# somehow?
f = self.through._meta.get_field_by_name(lookup_keys[0])[0]
f = _get_field(self.through, lookup_keys[0])
objs = f.rel.to._default_manager.filter(**{
"%s__in" % f.rel.field_name: [r["content_object"] for r in qs]
})
Expand Down Expand Up @@ -389,10 +389,10 @@ def related_query_name(self):
return _model_name(self.model)

def m2m_reverse_name(self):
return self.through._meta.get_field_by_name("tag")[0].column
return _get_field(self.through, 'tag').column

def m2m_reverse_field_name(self):
return self.through._meta.get_field_by_name("tag")[0].name
return _get_field(self.through, 'tag').name

def m2m_target_field_name(self):
return self.model._meta.pk.name
Expand Down Expand Up @@ -430,7 +430,7 @@ def get_extra_join_sql(self, connection, qn, lhs_alias, rhs_alias):
alias_to_join = rhs_alias
else:
alias_to_join = lhs_alias
extra_col = self.through._meta.get_field_by_name('content_type')[0].column
extra_col = _get_field(self.through, 'content_type').column
content_type_ids = [ContentType.objects.get_for_model(subclass).pk for
subclass in _get_subclasses(self.model)]
if len(content_type_ids) == 1:
Expand All @@ -449,8 +449,8 @@ def get_extra_join_sql(self, connection, qn, lhs_alias, rhs_alias):
# This and all the methods till the end of class are only used in django >= 1.6
def _get_mm_case_path_info(self, direct=False):
pathinfos = []
linkfield1 = self.through._meta.get_field_by_name('content_object')[0]
linkfield2 = self.through._meta.get_field_by_name(self.m2m_reverse_field_name())[0]
linkfield1 = _get_field(self.through, 'content_object')
linkfield2 = _get_field(self.through, self.m2m_reverse_field_name())
if direct:
join1infos = linkfield1.get_reverse_path_info()
join2infos = linkfield2.get_path_info()
Expand All @@ -465,8 +465,8 @@ def _get_gfk_case_path_info(self, direct=False):
pathinfos = []
from_field = self.model._meta.pk
opts = self.through._meta
object_id_field = opts.get_field_by_name('object_id')[0]
linkfield = self.through._meta.get_field_by_name(self.m2m_reverse_field_name())[0]
object_id_field = _get_field(self.through, 'object_id')
linkfield = _get_field(self.through, self.m2m_reverse_field_name())
if direct:
join1infos = [PathInfo(self.model._meta, opts, [from_field], self.rel, True, False)]
join2infos = linkfield.get_path_info()
Expand Down Expand Up @@ -496,7 +496,7 @@ def get_joining_columns(self, reverse_join=False):
return (("object_id", "id"),)

def get_extra_restriction(self, where_class, alias, related_alias):
extra_col = self.through._meta.get_field_by_name('content_type')[0].column
extra_col = _get_field(self.through, 'content_type').column
content_type_ids = [ContentType.objects.get_for_model(subclass).pk
for subclass in _get_subclasses(self.model)]
return ExtraJoinRestriction(related_alias, extra_col, content_type_ids)
Expand All @@ -506,8 +506,7 @@ def get_reverse_joining_columns(self):

@property
def related_fields(self):
return [(self.through._meta.get_field_by_name('object_id')[0],
self.model._meta.pk)]
return [(_get_field(self.through, 'object_id'), self.model._meta.pk)]

@property
def foreign_related_fields(self):
Expand All @@ -516,9 +515,11 @@ def foreign_related_fields(self):

def _get_subclasses(model):
subclasses = [model]
for f in model._meta.get_all_field_names():
field = model._meta.get_field_by_name(f)[0]

if VERSION < (1, 8):
all_fields = (_get_field(model, f) for f in model._meta.get_all_field_names())
else:
all_fields = model._meta.get_fields()
for field in all_fields:
# Django 1.8 +
if (not RelatedObject and isinstance(field, OneToOneRel) and
getattr(field.field.rel, "parent_link", None)):
Expand Down
6 changes: 4 additions & 2 deletions taggit/models.py
Expand Up @@ -8,6 +8,8 @@
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import ugettext

from taggit.utils import _get_field

try:
from django.contrib.contenttypes.fields import GenericForeignKey
except ImportError: # django < 1.7
Expand Down Expand Up @@ -101,11 +103,11 @@ class Meta:

@classmethod
def tag_model(cls):
return cls._meta.get_field_by_name("tag")[0].rel.to
return _get_field(cls, 'tag').rel.to

@classmethod
def tag_relname(cls):
return cls._meta.get_field_by_name('tag')[0].rel.related_name
return _get_field(cls, 'tag').rel.related_name

@classmethod
def lookup_kwargs(cls, instance):
Expand Down
8 changes: 8 additions & 0 deletions taggit/utils.py
@@ -1,10 +1,18 @@
from __future__ import unicode_literals

from django import VERSION
from django.utils import six
from django.utils.encoding import force_text
from django.utils.functional import wraps


def _get_field(model, name):
if VERSION < (1, 8):
return model._meta.get_field_by_name(name)[0]
else:
return model._meta.get_field(name)


def parse_tags(tagstring):
"""
Parses tag input, with multiple word input being activated and
Expand Down