进行以下测试:
贝壳:
from app.models import Campaign
from django.contrib.auth import get_user_model
user = get_user_model().objects.get(pk=1)
link_db = Link()
link_db.link = "test"
link_db.save()
Campaign(status="Pending", domain="www.test.org", user=user, link=link_db).save()
我收到错误异常:GFK.get_content_type不可能的参数!
为什么呢?我没有看到任何错误,也不知道这意味着什么。
广告系列:
class Campaign(models.Model):
status = models.CharField(max_length=16, choices=MESSAGE_STATUSES, default="Pending",)
domain = models.CharField(max_length=150)
user = models.ForeignKey(get_user_model(), related_name='campaign', null=True,
link = models.ForeignKey(Link, related_name='campaign', null=True)
链接:
class Link(models.Model):
link = models.URLField()
hits = models.IntegerField(default=0)
def __repr__(self):
return "<Link (Hits %s): %s>"%(self.hits, self.link)
def get_short_id(self):
_id = self.id
digits = []
while _id > 0:
rem = _id % 62
digits.append(rem)
_id /= 62
digits.reverse()
return index_to_char(digits)
@staticmethod
def decode_id(string):
i = 0
for c in string:
i = i * 64 + _char_map.index(c)
return i
完整筹码:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/user/Documents/workspace/test/django-env/lib/python2.7/site-packages/django/db/models/base.py", line 327, in __init__
signals.pre_init.send(sender=self.__class__, args=args, kwargs=kwargs)
File "/Users/user/Documents/workspace/test/django-env/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 170, in send
response = receiver(signal=self, sender=sender, **named)
File "/Users/user/Documents/workspace/test/django-env/lib/python2.7/site-packages/django/contrib/contenttypes/generic.py", line 50, in instance_pre_init
kwargs[self.ct_field] = self.get_content_type(obj=value)
File "/Users/user/Documents/workspace/test/django-env/lib/python2.7/site-packages/django/contrib/contenttypes/generic.py", line 63, in get_content_type
raise Exception("Impossible arguments to GFK.get_content_type!")
Exception: Impossible arguments to GFK.get_content_type!
答案 0 :(得分:3)
您不能在模型定义中使用get_user_model
,因为可以在设置所有模型之前对它们进行评估。相反,您应该直接引用settings.AUTH_USER_MODEL
,如in the documentation所述。
答案 1 :(得分:0)
我刚遇到同样的错误消息。是使用GenericForeignKey
字段将代码库从django 1.8迁移到1.6并将实际对象传递给create
函数,并期望Generic类通过以下方式查找content_type
和object_id
本身来自content_object
,但我发现它没有。它在1.8中运行得很好,但在1.6中,您似乎必须自己提取content_type
和object_id
并将它们传递到create
函数中。
对于模型:
class SomeObject(models.Model):
content_type = models.ForeignKey(ContentType, null=True, blank=True, )
object_id = models.PositiveIntegerField(null=True, blank=True)
content_object = generic.GenericForeignKey('content_type', 'object_id')
所以在1.6
content_object = SomeOtherObject.create(first_name='Foo', last_name='bar')
SomeObject.objects.create(
content_type=ContentType.objects.get_for_model(content_object),
object_id=content_object.pk
)
并且在1.8
content_object = SomeOtherObject.create(first_name='Foo', last_name='bar')
SomeObject.objects.create(
content_object=content_object
)
我尝试在pre_save
和pre_init
方法中解决此问题,但这会在调用它们之前发生。
希望它可以帮助别人!