Django管理界面从另一个与外键链接的表中过滤

时间:2012-02-04 21:53:00

标签: django django-models django-admin

我已经给出了两张桌子及其模型。

mysql> SELECT gid, sk, source from datagen_gidskmap limit 10;
+-----+------+----------+
| gid | sk   | source   |
+-----+------+----------+
|   1 | 3829 | smsarena |
|   2 | 623  | smsarena |
|   3 | 1308 | smsarena |
|   4 | 1747 | smsarena |
|   5 | 1827 | smsarena |
|   6 | 1218 | smsarena |
|   7 | 2957 | smsarena |
|   8 | 3468 | smsarena |
|   9 | 2580 | smsarena |
|  10 | 2579 | smsarena |
+-----+------+----------+
10 rows in set (0.00 sec)

class GidSkMap(models.Model):
    gid = models.AutoField(primary_key=True)
    sk  = models.CharField(max_length=256)
    source = models.CharField(max_length=64)
    creation_date = models.DateTimeField(auto_now_add=True)
    modification_date = models.DateTimeField(auto_now=True)


    def __unicode__(self):
        return u'%s' % self.gid

    class Meta:
        unique_together = ("sk", "source")

mysql> SELECT id, gid_id, nm from datagen_crawlmeta limit 10;
+----+--------+----------------------------+
| id | gid_id | nm                         |
+----+--------+----------------------------+
|  1 |      1 | votes                      |
|  2 |      1 | performance_rating         |
|  3 |      1 | title                      |
|  4 |      1 | specs__Sound__Loudspeaker  |
|  5 |      1 | specs__Sound__3.5mm jack   |
|  6 |      1 | specs__Sound__Alert types  |
|  7 |      1 | specs__Sound__unknown0     |
|  8 |      1 | specs__Features__Java      |
|  9 |      1 | specs__Features__Messaging |
| 10 |      1 | specs__Features__Colors    |
+----+--------+----------------------------+
10 rows in set (0.00 sec)

class CrawlMeta(models.Model):
    gid = models.ForeignKey(GidSkMap)
    nm = models.CharField(max_length=256)
    val = models.TextField()
    modification_date = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return u'%s' % self.gid

    class Meta:
        unique_together = ("gid", "nm")

在django管理界面中查看CrawlMeta模型时, 我想有一个基于“源”(例如:smsarena)的过滤器,可以访问 通过gid(这是CrawlMeta中的foriegn键和GidSkMap中的primary_key)。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

list_filter接受queryset API样式字段查找。在这种情况下,您将使用:

class YourAdmin:
    list_filter = ["gid__source", ... ]
    list_display = ["gid_source", ... ]