2013-11-15 37 views
3

編輯: 我已經決定我的問題改變這一點,希望更一般的問題會更吸引人......Django的 - 排序在管理用戶界面模型根據譯名

所以我有一個模型,它有一個不在服務器中翻譯的M2M字段,但它在客戶端。我可以在admin-ui中排序此字段,特別是每次添加模型的新實例時?

這是我最後一次嘗試:

感興趣的機型有:

class Recipe(models.Model): 
    name = models.CharField(max_length=200,verbose_name=_("name")) 
    ingredient_list = models.ManyToManyField(IngredientType, through='Ingredient') 

class IngredientType(models.Model): 
    name = models.CharField(max_length=25) 

class Ingredient(models.Model): 
    ingredient_type =models.ForeignKey(IngredientType,verbose_name=_("IngredientType")) 
    recipe = models.ForeignKey(Recipe) 

recipe_admin.py:

class IngredientInline(admin.TabularInline): 
    model = Ingredient 
    extra = 1 

class RecipeAdmin(admin.ModelAdmin): 
    inlines = [IngredientInline] 
    form = RecipeForm 

class RecipeForm(forms.ModelForm): 
    class Meta: 
     model = Recipe 

我已在IngredientType對象一長串我initial_data.json文件。 現在,每當有人在他的配方中添加一種新配料時,他就會從打開的長下拉列表中選擇一種配料類型。

編輯: 在瀏覽器中查看窗體的html時,我的下拉列表中有這些行。 正如你所看到的,在該字段的下面有一行負責使用showAddAnotherPopup(this)函數向內聯添加另一行: (我想以某種方式掛鉤到該函數中,並且每次調用它時調用我的排序功能)

<select id="id_ingredient_set-0-ingredient_type" name="ingredient_set-0-ingredient_type"> 
<option value="" selected="selected">---------</option> 
<option value="42">אבוקדו</option> 
<option value="80">אבטיח</option> 
.... 
</select> 
<a href="/admin/vcb/ingredienttype/add/" class="add-another" id="add_id_ingredient_set-0-ingredient_type" onclick="return showAddAnotherPopup(this);"> <img src="/static/admin/img/icon_addlink.gif" width="10" height="10" alt="Add another"></a> 

我的問題是,用戶增加了他的配方形式動態地添加成分行,結果在下拉ingredient_type名稱列表是無序。我能夠按照希伯來語字母順序進行排序,但是我已經設法只爲頁面加載時已經顯示的線條這樣做。

function sortAlpha(a,b){ 
return a.innerHTML.toLowerCase() > b.innerHTML.toLowerCase() ? 1 : -1; 
}; 

,我調用它像這樣:

我由Django的tabular.html文件遷移到我的項目文件系統,並增加了一些JS的排序它做上述

('#id_ingredient_set-0-ingredient_type option')。sort(sortAlpha).appendTo('#id_ingredient_set-0-ingredient_type');

在頁面加載時觸發的函數內部。 但是這種方法顯然不處理用戶添加到他的配方表單中的所有動態添加的成分行,這導致下拉的成分類型名稱列表未被分類。 (它掛鉤在本ID)

這是Django的showAddAnotherPopup方法,我也加入到我的項目的文件系統:

function showAddAnotherPopup(triggeringLink) { 
    var name = triggeringLink.id.replace(/^add_/, ''); 
    name = id_to_windowname(name); 
    href = triggeringLink.href 
    if (href.indexOf('?') == -1) { 
     href += '?_popup=1'; 
    } else { 
     href += '&_popup=1'; 
    } 
    var win = window.open(href, name, 'height=500,width=800,resizable=yes,scrollbars=yes'); 
    win.focus(); 
    return false; 
} 

我是一個Django的小白,所以我還是在所有認識不足AJAX/js/templatetag buzz-words ... 幫助將不勝感激。 TNX Nitzan

+1

如果有更多的信息缺失,我會很樂意提供。 – nitzanwe

回答

0

這不是你的榜樣,你是如何觸發的初始頁面加載的排序功能明確,但似乎你需要再次調用該函數每添加一個新的項目之後。添加onclick,onblur或其他事件觸發器到成分添加方法,將再次調用您的分類功能。

如果您嘗試訂購由模型窗體創建的項目的顯示列表,您可以使用元選項orderorder_with_respect_to。有關更多信息,請參閱https://docs.djangoproject.com/en/dev/ref/models/options/#ordering

+0

我不能使用order或order_with_respect_to選項,因爲它們使用db上的數據,而我的ingredient_type沒有在那裏翻譯。正如我的EDIT暗示的,我知道我需要添加一些onClick方法,或以某種方式覆蓋django的showAddAnotherPopup(this)。 – nitzanwe

+0

但我不知道怎麼做,因此我的問題.. :) – nitzanwe

+0

您是否嘗試將onclick更改爲執行showAddAnotherPopup(this)的新函數調用,然後resortFunction()然後返回「? 如果HTML是由Django管理界面創建的,可能非常複雜,因爲你必須創建自己的壓倒一切的視圖版本 – ChrisFreeman