如何在 django 中编写连接?

发布于 2024-10-14 20:10:51 字数 2818 浏览 3 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月竹挽风 2024-10-21 20:10:51

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

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

从您链接到的文档中:

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

这意味着:

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 your working model doesn't have a name field.

From the docs you linked to:

Django offers a powerful and intuitive
way to "follow" relationships in
lookups, taking care of the SQL JOINs
for you automatically, behind the
scenes. To span a relationship, just
use the field name of related fields
across models, separated by double
underscores
, until you get to the
field you want.

That translates to:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文