【问题标题】:How to write joins in django?如何在 django 中编写连接?
【发布时间】:2011-06-16 23:15:35
【问题描述】:

如何在 django 中编写连接,我已经阅读了下面的 django 文档,但是连接不适用于我的模型

http://docs.djangoproject.com/en/1.2/topics/db/queries/#lookups-that-span-relationships

和模型/many_to_many/

还有一些博客

我的模特:

class Profile(models.Model):
     name = models.CharField(max_length=50, primary_key=True)
     assign = models.CharField(max_length=50)
     doj = models.DateField()
     dob = models.DateField()

     class Meta:
        db_table = u'profile'

     def __str__(self):
         return  '%s %s %s %s' % ( self.name,self.assign,self.doj,self.dob)

     def __unicode__(self):
         return  u'%s %s %s %s' % ( self.name,self.assign,self.doj,self.dob)


     enter code here

class working(models.Model):
   w_name =models.ForeignKey(Profile, db_column='w_name')
   monday =  models.IntegerField(null=True, db_column='monday', blank=True)
   tuesday =  models.IntegerField(null=True, db_column='tuesday', blank=True)
   wednesday =  models.IntegerField(null=True, db_column='wednesday', blank=True)

   class Meta:
        db_table = u'working'

   def __str__(self):
         return  '%s %s %s %s' % ( self.w_name,self.monday,self.tuesday,self.wednesday)

   def __unicode__(self):
         return  u'%s %s %s %s' % ( self.w_name,self.monday,self.tuesday,self.wednesday)

我正在尝试在两个表配置文件和工作日之间进行连接

like m=working.objects.filter(name='sushanth').select_related()

如果我运行上面的查询,我会得到

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/lib/pymodules/python2.6/django/db/models/manager.py", line 129, in filter
    return self.get_query_set().filter(*args, **kwargs)
  File "/usr/lib/pymodules/python2.6/django/db/models/query.py", line 498, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/usr/lib/pymodules/python2.6/django/db/models/query.py", line 516, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/usr/lib/pymodules/python2.6/django/db/models/sql/query.py", line 1675, in add_q
    can_reuse=used_aliases)
  File "/usr/lib/pymodules/python2.6/django/db/models/sql/query.py", line 1569, in add_filter
    negate=negate, process_extras=process_extras)
  File "/usr/lib/pymodules/python2.6/django/db/models/sql/query.py", line 1737, in setup_joins
    "Choices are: %s" % (name, ", ".join(names)))
FieldError: Cannot resolve keyword 'name' into field. Choices are:  monday,  tuesday, wednesday,  w_name

我需要查询我可以在哪里加入工作和个人资料。

支持

select working.*,profile.assign,profile.doj from working join profile where name=w_name ;

我知道 django 不支持 join,inner join 对我来说也可以。

任何人都可以帮助解决这个问题............?

【问题讨论】:

  • @user593132:请格式化您的问题,以便代码可读({} 按钮),否则很难阅读。
  • 让我建议您为working 模型取一个更好的名称:WorkSchedule。始终使用 CamelCase 作为模型名称。您的 w_name 列应命名为 profile,它指向配置文件,而不是名称。如果没有大的原因,请不要更改 db_columnt、db_table 名称。
  • 现在代码是可读格式

标签: django django-models django-queryset


【解决方案1】:

请格式化您的问题,以便代码可读({} 图标),否则很难阅读。

working.objects.filter(name= 将失败,因为您的 working 模型没有 name 字段。

来自您链接到的文档:

Django 提供了一个强大而直观的 “跟随”关系的方法 查找,处理 SQL JOIN 自动为你,背后 场景。要跨越一段关系,只需 使用相关字段的字段名 跨模型,以双精度分隔 下划线,直到您到达 你想要的字段。

意思是:

m = working.objects.filter(w_name__name='sushanth').select_related()

【讨论】:

  • 首先感谢您的回复,我已将代码更改为可读格式。我在上面尝试仍然遇到同样的错误......在此先感谢
  • &gt;&gt; from testapp.mod.models import * &gt;&gt;&gt; m=working.objects.filter(profile__name='sushanth') Traceback(最近一次调用最后):文件“<console>”,第 1 行,在 <module> 文件“/usr/lib/pymodules/python2.6/django/db/models/sql/query.py”中,第 1737 行,在 setup_joins “选择是:%s” % ( name, ", ".join(names))) FieldError:无法将关键字“profile”解析到字段中。选项有:id、monday、tuesday、w_name、wednesday</module></console>
  • >>> m=working.objects.filter(w_name__name='sushanth') >>> m.query.as_sql() (u'SELECT working.id, working .w_name, working.monday, working.tuesday, working.wednesday FROM working WHERE working.w_name,',' ))
  • 它没有给出任何连接查询
  • m = working.objects.filter(w_name__name='sushanth').select_related()
猜你喜欢
  • 1970-01-01
  • 2016-10-16
  • 1970-01-01
  • 2018-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多