2012-02-04 64 views
0

我給出了兩個表格及其模型。Django管理界面過濾器與另外一個與外鍵鏈接的表格

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)。

任何幫助,將不勝感激。

回答

0

list_filter接受查詢集API樣式字段查找。在這種情況下,您可以使用:

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

謝謝托馬斯。有用。有沒有辦法在表格中顯示源列?我通過在模型中使用'short_description'添加。但它不允許在源代碼上進行排序。 – 2012-02-05 17:46:17

+0

你應該可以將「gid__source」添加到「list_display」並讓它自動排序。 – Thomas 2012-02-05 22:44:10