最近我已经实现了一个临时django模型,我希望当用户从django admin点击它时填充它
这是django admin
中的实现 from django.contrib import admin
from polls.models import Poll
from polls.models import TempModel
from django.conf.urls import patterns
from django.http import HttpResponse
from test_data import TestData
from django.http import HttpResponse
from django.template import RequestContext, loader
from test_data import TestData
class TempModelAdmin(admin.ModelAdmin):
fields = ('permalink', )
def test(self):
x = TestData.get_test_data()
admin.site.register(Poll)
admin.site.register(TempModel, TempModelAdmin)
这是我用来填充数据的临时类
from models import TempModel
class TestData(object):
@classmethod
def get_test_data(self):
print "**********************************************"
print "get test data"
print "**********************************************"
list = []
for x in range(0,50):
str_val = str(x) + "djdj;djfhdfjiosdifj";
list.append(str_val)
temp_model = TempModel()
temp_model.permalink = str_val
temp_model.save()
print "=============================================="
print "Object Count"
print TempModel.objects.count()
print "=============================================="
return list
这不是被召唤的。任何人都可以知道这个的原因吗? 谢谢你的推荐
答案 0 :(得分:0)
你正在用self创建一个类方法,它需要一个类型的实例。尝试更改方法签名:
def get_test_data(cls):
答案 1 :(得分:0)
我通过这种方法想出来并且它有效
class TempModelAdmin(admin.ModelAdmin):
fields = ('permalink', )
def __init__(self, *args, **kwargs):
super(TempModelAdmin, self).__init__(*args, **kwargs)
self.my_method()
def my_method(self):
print "*************----------------------------"
TestData.get_test_data()