django ForeignKey model filter in admin-area?

末鹿安然 提交于 2019-12-08 07:29:19

Try redifinition unicode method.

def __unicode__(self):
        return self.student_city 

So you'll see in the field student city.

Well, I tried to remake your application to set data with forms class. Something like this in admin.py in your application:

from django.contrib import admin
from django import forms
from myapp.models import *

class ClassRoomsAdminForm(forms.ModelForm):
    class Meta:
        model = ClassRoom
    def __init__(self, *arg, **kwargs):
        super(ClassRoomsAdminForm, self).__init__(*arg, **kwargs)
        self.fields[' class_student_cities'].choices = [(csc.id,csc.student_city) for csc in Students.objects.all()

class ClassRoomsAdmin(admin.ModelAdmin):
    form = ClassRoomsAdminForm

admin.site.register(ClassRooms,ClassRoomsAdmin)

Maybe you'll need to fix something, but I hope it will work. You will set init function to your forms, so in admin panel you set all choices to everything you keep in your Students model. csc.id you'll need to make this object iterable (cities aren't unique) and then you can choose everything from Students model to set in the field.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!