2013-11-15 3 views
3

편집 번역 이름에 따라 관리자 UI에서 모델을 정렬 : 내가 좀 더 일반적인 질문은 더 매력적이 될 것이라는 희망이 내 질문을 변경하기로 결정했습니다 ...장고는

그래서 모델을 가지고 있지만 서버에 번역되지 않은 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 : 나는에 IngredientType 객체의 긴 목록을 추가 한

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

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

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

내 initial_data.json 파일. 이제 사람이 자신의 요리법에 새 재료를 추가 할 때마다 열리는 긴 드롭 다운 목록에서 재료 유형을 선택합니다.

편집 : 브라우저에서 양식의 html을 보면 드롭 다운 목록에이 줄이 있습니다. 보시다시피 해당 필드 아래에는 showAddAnotherPopup (this) 함수를 사용하여 인라인에 다른 행을 추가하는 행이 있습니다. (이 함수에 연결하고 싶습니다. 호출 될 때마다 내 정렬을 호출합니다. 기능), 사용자는 자신의 레시피에 추가하는 형태로 동적으로 추가 성분 행 드롭 다운 ingredient_type 이름에 정렬되지 않은 결과로 나열

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

내 문제가된다. 나는 히브리어 알파벳 순으로 정렬 할 수 있지만, 페이지가로드 될 때 이미 명백한 선에서만 그렇게 할 수있었습니다.

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

내가 그렇게처럼 전화 했어 :

나는 그것을 정렬 일부 JS 내 프로젝트 파일 시스템으로 장고의 tabular.html 파일을 마이그레이션하고, 추가하여 위를했던

('# id_ingredient_set-0-ingredient_type 옵션') sort (sortAlpha) .appendTo ('# id_ingredient_set-0-ingredient_type');

페이지가로드 될 때 발생하는 기능. 하지만이 방법은 분명히 사용자가 레시피 양식에 추가하는 모든 동적 추가 된 성분 행을 처리하지 않으므로 드롭 다운 성분 _ 유형 이름 목록이 정렬되지 않습니다. (그것은에서 본 ID를 후크)

이것은 나 또한 내 프로젝트의 파일 시스템에 추가 장고의 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; 
} 

내가 장고 멍청한 놈, 그래서 나는 아직도 모든 지식 부족 AJAX/js/templatetag buzz-words ... 도움을 많이 주시면 감사하겠습니다. tnx Nitzan

+1

더 많은 정보가 누락되면 기꺼이 제공해 드리겠습니다. – nitzanwe

답변

0

초기 페이지로드시 정렬 기능을 트리거하는 방법은 명확하지 않지만 새 항목을 추가 할 때마다 함수를 다시 호출해야합니다.onclick, onblur 또는 다른 이벤트 트리거를 정렬 함수를 다시 호출하는 성분 추가 메소드에 추가하십시오.

modelform에 의해 만들어진 표시된 항목 목록을 주문하려는 경우 메타 옵션 order 또는 order_with_respect_to을 사용할 수 있습니다. 자세한 내용은 https://docs.djangoproject.com/en/dev/ref/models/options/#ordering을 참조하십시오.

+0

db의 데이터를 사용하기 때문에 orders_type이 번역되지 않았기 때문에 order_with_respect_to 옵션을 사용할 수 없습니다. 내 EDIT 암시, 나는 일부 onClick 메서드를 추가 할 필요가 어떻게 든 django의 showAddAnotherPopup (this) 재정의 할 필요가 이해합니다. – nitzanwe

+0

하지만 어떻게 해야할지 모르겠다. 그러므로 내 질문은 .. :) – nitzanwe

+0

showAddAnotherPopup (this)을 수행하는 새 함수 호출로 onclick을 변경해 본 다음 resortFunction()이 "? 을 반환하면 Django admin 인터페이스 해킹에 의해 HTML이 생성되는 중입니다.보기의 우선하는 버전을 생성해야하므로 매우 복잡 할 수 있습니다. – ChrisFreeman