使用 select_related 时出现非常奇怪的错误。我创建了一个扩展 auth_user 表的模型。供您参考,我在下面粘贴模型
class BasicDetails(models.Model):
username = models.OneToOneField(User)
name = models.CharField(max_length = 100, verbose_name = "Name")
sex = models.CharField(max_length = 10, verbose_name = "Sex", choices = GENDER_CHOICES)
dob = models.DateField(verbose_name = "Date of Birth")
mothertongue = models.CharField(max_length = 20, verbose_name = "Mother Tongue", choices = LANGUAGES_CHOICES)
上表只有一条记录,当我在 django shell 中运行以下查询时,它工作正常,我能够检索这些值。
basicdetails = BasicDetails.objects.select_related('auth_user__username',request.user)
但是,当我在视图和模板中输入相同的查询时,我遇到了错误。
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
111.response = callback(request, *callback_args, **callback_kwargs)
File "/home/vikramt/python/OpenMatrimony/wedding/views.py" in myprofile
160.basicdetails = BasicDetails.objects.select_related('auth_user__username',request.user)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py" in select_related
173.return self.get_query_set().select_related(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in select_related
686.obj.query.add_select_related(fields)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py" in add_select_related
1759.for part in field.split(LOOKUP_SEP):
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py" in inner
185.return func(self._wrapped, *args)
异常类型:/myprofile/ 处的 AttributeError 异常值:“用户”对象没有属性“拆分”
有人可以在这种情况下帮助我吗?我无法理解问题所在。有什么线索吗?
-维克拉姆