편집 번역 이름에 따라 관리자 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
더 많은 정보가 누락되면 기꺼이 제공해 드리겠습니다. – nitzanwe