我想在contains
查询中使用join
,但它会出错。
以下是我的 models.py :
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)
我想使用这样的东西:
m = working.objects.filter(w_name__name__contains='sushanth')
or
m = working.objects.filter(w_name__name__startswith='sushanth')
我一直得到的错误:
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.6/dist-packages/Django-1.2.4-py2.6.egg/django/db/models/manager.py", line 141, in filter
return self.get_query_set().filter(*args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/Django-1.2.4-py2.6.egg/django/db/models/query.py", line 556, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/Django-1.2.4-py2.6.egg/django/db/models/query.py", line 574, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/usr/local/lib/python2.6/dist-packages/Django-1.2.4-py2.6.egg/django/db/models/sql/query.py", line 1152, in add_q
can_reuse=used_aliases)
File "/usr/local/lib/python2.6/dist-packages/Django-1.2.4-py2.6.egg/django/db/models/sql/query.py", line 1092, in add_filter
connector)
File "/usr/local/lib/python2.6/dist-packages/Django-1.2.4-py2.6.egg/django/db/models/sql/where.py", line 67, in add
value = obj.prepare(lookup_type, value)
File "/usr/local/lib/python2.6/dist-packages/Django-1.2.4-py2.6.egg/django/db/models/sql/where.py", line 316, in prepare
return self.field.get_prep_lookup(lookup_type, value)
File "/usr/local/lib/python2.6/dist-packages/Django-1.2.4-py2.6.egg/django/db/models/fields/related.py", line 139, in get_prep_lookup
raise TypeError("Related Field has invalid lookup: %s" % lookup_type)
TypeError: Related Field has invalid lookup: startswith
有什么建议吗?
答案 0 :(得分:1)
名称字段是主键有什么原因吗?
如果没有,那么我建议使用名称字段NOT作为主键重新创建表。这可能会抛弃过滤器逻辑。