Django 查询与 Q 对象?

发布于 2024-08-16 04:18:22 字数 1155 浏览 6 评论 0 原文

我有一个模型,

class Employee_Type(models.Model):
    def __unicode__(self):
        return self.name
    name = models.CharField(max_length=200, verbose_name="employee type")

class Employee(models.Model):
    def __unicode__(self):
        return self.name
    name = models.CharField(max_length=200)
    type = models.ForeignKey(Employee_Type)
    address = models.CharField(max_length=500,blank=True, null=True)
    telephone = models.CharField(max_length=100, blank=True, null=True)
    fax = models.CharField(max_length=100, blank=True, null=True)
    email = models.EmailField(max_length=200, blank=True, null=True)
    active = models.BooleanField(default=True) 

我需要查询如下内容:

 employees = Employee.objects.filter(
                            Q(name__startswith=key_search) \
                            & Q(type__icontian= emp_type)#CAN I DO THIS?
                            Q(active=True)                            
                            )

问题:

Q(type__= emp_type) (type = models.ForeignKey(Employee_Type)) I cannot do this.

这里有人请帮助我吗?

I have a model

class Employee_Type(models.Model):
    def __unicode__(self):
        return self.name
    name = models.CharField(max_length=200, verbose_name="employee type")

class Employee(models.Model):
    def __unicode__(self):
        return self.name
    name = models.CharField(max_length=200)
    type = models.ForeignKey(Employee_Type)
    address = models.CharField(max_length=500,blank=True, null=True)
    telephone = models.CharField(max_length=100, blank=True, null=True)
    fax = models.CharField(max_length=100, blank=True, null=True)
    email = models.EmailField(max_length=200, blank=True, null=True)
    active = models.BooleanField(default=True) 

I need to query something like this:

 employees = Employee.objects.filter(
                            Q(name__startswith=key_search) \
                            & Q(type__icontian= emp_type)#CAN I DO THIS?
                            Q(active=True)                            
                            )

Problems:for

Q(type__= emp_type) (type = models.ForeignKey(Employee_Type)) I cannot do this.

Anybody here Please help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

谜兔 2024-08-23 04:18:22

Employee_Type 应重命名为 EmployeeType。这是 Django 模型名称约定。基础表将创建为 appname_employee_type

对于直接的 and 条件,您不需要 Q() 对象。 Q() 对象对于 or 条件或组合 andor 非常有用。

那么您的查询将是:

employees = Employee.objects.filter(name__startswith=key_search, 
                                    type=emp_type, 
                                    active=True)                            

当然,假设变量 emp_type 包含 EmployeeType 的实例。如果 emp_type 表包含员工类型的名称,请使用:

employees = Employee.objects.filter(name__startswith=key_search, 
                                    type__name=emp_type, 
                                    active=True)                            

Employee_Type should be renamed to EmployeeType. This is the Django convention for model names. The underlying table will be created as appname_employee_type.

You don't need Q() objects for straight and conditions. Q() objects are useful for or conditions, or for combining ands and ors.

Then your query will be:

employees = Employee.objects.filter(name__startswith=key_search, 
                                    type=emp_type, 
                                    active=True)                            

Assuming, of course, the variable emp_type contains an instance of EmployeeType. If the emp_type table contains the name of an employee type, use:

employees = Employee.objects.filter(name__startswith=key_search, 
                                    type__name=emp_type, 
                                    active=True)                            
流殇 2024-08-23 04:18:22

如果将 Employee_Type 重命名为 Employeetype,则以下操作可能有效:

Employee.objects.filter(employeetype__name=emp_type, name__startswith=key_search, active=True)

(您可以在filter()<中使用多个条件< /code>,应用 AND 运算符。)

if you rename Employee_Type to Employeetype, following might work:

Employee.objects.filter(employeetype__name=emp_type, name__startswith=key_search, active=True)

(you can use multiple conditions in filter(), AND operator is applied.)

行雁书 2024-08-23 04:18:22

Q 对象最适合动态查询构建;有关涵盖此主题和其他主题的 Q 对象的教程可以在此处找到:djangos-q-objects 的力量

Q objects a best used for dynamic query building; a tutorial on Q objects covering this and other topics can be found here: the-power-of-djangos-q-objects

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