如何在 django 中编写连接?
如何在 django 中编写联接,我已经阅读了下面的 django 文档,但是联接不适用于我的模型
http://docs.djangoproject.com/en/1.2/topics/db/queries/#lookups-that-span-relationships
和 models/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
我需要查询可以在哪里连接工作和配置文件。
支持
从工作连接配置文件中选择工作。*,profile.assign,profile.doj,其中name=w_name;
我知道django不支持连接,内部连接对我来说也可以。
任何人都可以帮忙解决这个问题吗............?
How to write joins in django,i have gone through below django documentation ,but joins are not working for my model
http://docs.djangoproject.com/en/1.2/topics/db/queries/#lookups-that-span-relationships
and models/many_to_many/
and some blogs
My Model :
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)
I am trying to do join between two tables profile and workingday
like m=working.objects.filter(name='sushanth').select_related()
if i run above query i'll get
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
I need to query where i can join working and profile.
support
select working.*,profile.assign,profile.doj from working join profile where name=w_name ;
I know django won't support joins,inner join is also okay for me.
Can any one help on this.........................?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请格式化您的问题,以便代码可读({} 图标),否则很难阅读。
working.objects.filter(name=
将失败,因为您的working
模型没有name
字段。从您链接到的文档中:
这意味着:
m =working.objects.filter(w_name__name='sushanth').select_lated()
Please format your question so that the code is readable (the {} icon) otherwise it is very hard to read.
working.objects.filter(name=
will fail because yourworking
model doesn't have aname
field.From the docs you linked to:
That translates to:
m = working.objects.filter(w_name__name='sushanth').select_related()