开发者

How to create a personalized Django admin model?

开发者 https://www.devze.com 2023-02-02 10:24 出处:网络
I am writing a Django application and I need to create a personalized admin model, in order to show only determined items in the administration interface when you create a new object.

I am writing a Django application and I need to create a personalized admin model, in order to show only determined items in the administration interface when you create a new object.

I wrote this code for admin.py:

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

class SimAdmin(admin.ModelAdmin):
    phone = forms.ModelChoiceField(queryset=Item.objects.filter(name='phone'))
    fields = ('phone', 'num_phone', 'pin', 'puk')

admin.site.register(Item)
admin.site.register(Sim, SimAdmin)
...

The problem is that when I log in to the admin site, my SimAdmin does not appear. Django does not give me any error in this code, but if I write only

...
admin.site.register(SimAdmin)
...

it gives me a

开发者_如何学JAVA

TypeError: 'MediaDefiningClass' object is not iterable

in this line.

I searched in the web and the documentation for this error, but I didn't found anything relevant about my concrete problem. I think the solution will be very simple, but I can't see it. Can anybody help me?

Thanks!


this is incorrect:

class SimAdmin(admin.ModelAdmin):
    phone = forms.ModelChoiceField(queryset=Item.objects.filter(name='phone'))

you're trying to attach forms.Field to admin.ModelAdmin. It doesn't work this way.

Take a look at how can it be implemented: http://docs.djangoproject.com/en/1.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_manytomany

0

精彩评论

暂无评论...
验证码 换一张
取 消