Django管理员搜索非现有的返回所有项目

时间:2014-01-29 09:07:29

标签: python django django-admin

我有以下型号:

class FrontendUser(models.Model):

    CREDIT_RANGE = range(100, 199)

    class Meta:
        app_label = 'backoffice'
        db_table = 'users'

    uid = models.IntegerField(primary_key=True)
    mail = models.CharField(max_length=254)
    created = models.IntegerField()
    login = models.IntegerField()

使用以下管理界面:

class FrontendUserAdmin(admin.ModelAdmin):
    search_fields = ['mail', 'uid', 'login']
    list_display = ['customer_link', 'customer_email',  'mail',  'created_on',
                    'last_login_on', 'uid']
    readonly_fields = ['uid', 'created_on', 'login','created' ]
    actions = None

    def created_on(self, frontend_user):
        return datetime.datetime.utcfromtimestamp(frontend_user.created)

    def last_login_on(self, frontend_user):
        return datetime.datetime.utcfromtimestamp(frontend_user.login) if frontend_user.login else 'Never'

    def get_changelist(self, request, **kwargs):
        return CustomChangeList

    def customer_email(self, frontend_user):
        return u'<a href="/backoffice/frontenduser/%s">%s</a>' % (frontend_user.uid, frontend_user.mail)

    def customer_link(self, frontend_user):
        if frontend_user.uid != -1:
            return u'<a href="/customer/view/%s">%s</a>' % (frontend_user.uid, frontend_user.mail)
        else:
            return u'<a href="/customer/view/%s?mail_addr=%s">DOES NOT EXIST - %s</a>' % (frontend_user.uid, frontend_user.mail, frontend_user.mail)

    def has_add_permission(self, request):
        return False

    customer_link.allow_tags = True
    customer_email.allow_tags = True

    def __init__(self, *args, **kwargs):
        super(FrontendUserAdmin, self).__init__(*args, **kwargs)
        self.list_display_links = (None, )

当我在搜索字段中搜索不存在的电子邮件时,我通常没有结果。 除非我查找一些特殊的电子邮件(我不想在此处发送此电子邮件,它属于客户),我会在数据库中获得所有 59024 用户。

电子邮件是31个字符长

 verylongemailproblem@gmail.com

这足以让我更改电子邮件中的一个字符,然后Django将找不到这样的任何用户(如预期的那样)。我还直接用以下查询检查了数据库:

select * from users where mail like 'verylongemailproblem@gmail.com';
select * from users where login like 'verylongemailproblem@gmail.com';
select * from users where uid like 'verylongemailproblem@gmail.com';

以上所有查询均不返回任何用户。

更新

Django执行以下查询:

(0.000) SELECT "django_session"."session_key", "django_session"."session_data", "django_session"."expire_date" FROM "django_session" WH
ERE ("django_session"."session_key" = r3fdvb8kn2ub6blbyqpfziq017yddhpm  AND "django_session"."expire_date" > 2014-01-29 11:09:46.454778
 ); args=('r3fdvb8kn2ub6blbyqpfziq017yddhpm', u'2014-01-29 11:09:46.454778')
(0.000) SELECT "auth_user"."id", "auth_user"."password", "auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."username", 
"auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."d
ate_joined" FROM "auth_user" WHERE "auth_user"."id" = 1 ; args=(1,)
(0.057) SELECT `company_payment_item`.`cpid`, `company_payment_item`.`coid`, `company_payment_item`.`mail`, `company_payment_item`.`amount`
, `company_payment_item`.`package`, `company_payment_item`.`status`, `company_payment_item`.`timestamp`, `company_payment_item`.`vin`, `car
fax_payment_item`.`report_ref` FROM `company_payment_item` WHERE (`company_payment_item`.`coid` = 'verylongemailproble@gmail.com'  OR `c
arfax_payment_item`.`mail` = 'verylongemailproble@gmail.com' ); args=(u'verylongemailproble@gmail.com', u'verylongemailproble@gmail.
com')
(0.133) SELECT `users`.`uid`, `users`.`mail`, `users`.`created`, `users`.`login` FROM `users` WHERE ((`users`.`mail` LIKE '%verylongemailproblem@gmail.com%'  
OR `users`.`uid` LIKE '%verylongemailproble@gmail.com%'  OR `users`.`login` LIKE '%verylongemailproble@gmail.c
om%' ) OR `users`.`mail` = 'verylongemailproble@gmail.com' ) ORDER BY `users`.`uid` DESC; args=(u'%verylongemailproble@gmail.com%', u
'%verylongemailproble@gmail.com%', u'%verylongemailproble@gmail.com%', u'verylongemailproble@gmail.com')
(1.593) SELECT `users`.`uid`, `users`.`mail`, `users`.`created`, `users`.`login` FROM `users` ORDER BY `users`.`uid` DESC; args=()
(0.007) SELECT `backend_transaction`.`id`, `backend_transaction`.`t_ref_id`, `backend_transaction`.`account_id`, `backend_transaction`.
`sub_account_id`, `backend_transaction`.`role`, `backend_transaction`.`t_type`, `backend_transaction`.`requester_id`, `backend_transact
ion`.`condition_id`, `backend_transaction`.`rcresult_id`, `backend_transaction`.`report_id`, `backend_transaction`.`ext_t_ref`, `backen
d_transaction`.`qty`, `backend_transaction`.`expires_on`, `backend_transaction`.`created`, `backend_transaction`.`asset_id` FROM `backe
nd_transaction` WHERE `backend_transaction`.`t_ref_id` = 'verylongemailproble@gmail.com' ; args=(u'verylongemailproble@gmail.com',)
(0.026) SELECT COUNT(*) FROM `users`; args=()
  • 所以问题是为什么当我在Django中搜索该数据库时,我在表中获得了所有59024个条目?
  • 有些人可以提供一些如何解决问题的见解吗?

1 个答案:

答案 0 :(得分:1)

(0.133)是查找您提到的电子邮件的查询。 (1.593)正在选择所有用户。

第一个是返回所有结果,在这种情况下,mail,uid或login中的一个实际上持有verylongemailproble@gmail.com作为每个记录的子字符串。 或FrontendUserAdmin因某些其他原因返回每条记录。

在第二种情况下,也许django-debug-toolbar将显示此查询在django中运行的位置,这将帮助您挖掘代码。

调查您更有可能或更容易听到的声音。