我正在制作基于django管理员的项目,但由于此错误,我无法继续。
我的代码是
admin.py
from django.contrib import admin
from account.models import Account
from import_export import resources
from import_export.admin import ImportExportModelAdmin
class AccounttResource(resources.ModelResource):
class Meta:
model=Account
import_id_fields = ['cust_id']
class AccountAdmin(ImportExportModelAdmin,admin.ModelAdmin):
resource_class = AccounttResource
list_display = ['cust_first_name',]
readonly_fields=('image_tag_thumb','image_tag','cust_id',)
admin.site.register(Account,AccountAdmin)
models.py
from django.db import models
from datetime import date
from django.contrib import admin
from django.shortcuts import render ,redirect
from django.http import HttpResponse
from django.utils.safestring import mark_safe
from django.utils.translation import gettext as _
from django.contrib.admin.widgets import AdminFileWidget
# Create your models here.
class Account(models.Model):
cust_id=models.CharField(max_length=50,primary_key=True)
cust_first_name=models.CharField(max_length=50)
cust_middle_name=models.CharField(max_length=50,blank=True, null=True)
cust_last_name=models.CharField(max_length=50,blank=True, null=True)
father_name=models.CharField(max_length=50)
village_name=models.CharField(max_length=50)
crusher_choices=(
('1','12X14'),
('2','13X15'),
('3','13X16'),
('4','14X16'),
('5','14X17'),
('6','15X17'),
('7','15X18'),
('8','16X17'),
('9','16X18'),
('10','17X18'),
)
size_of_crusher=models.CharField(max_length=1,choices=crusher_choices,blank=True, null=True)
bowl_choices=(
('1','62'),
('2','68'),
('3','72'),
('4','74'),
)
size_of_bowl=models.CharField(max_length=1,choices=bowl_choices,blank=True, null=True)
rent_fixed=models.IntegerField(default=0)
reffered_by=models.CharField(max_length=50,blank=True, null=True)
contact_no1=models.CharField(max_length=50,blank=True, null=True)
contact_no2=models.CharField(max_length=50,blank=True, null=True)
address=models.CharField(max_length=120)
assigned_technician=models.CharField(max_length=50,blank=True, null=True)
first_deposit=models.PositiveIntegerField(default=0)
date_of_rent=models.DateField(blank=True, null=True)
total_payment_received=models.IntegerField(default=0)
balance=models.PositiveIntegerField(default=0,blank=True, null=True)
# item_replaced=
technician_visit=models.DateField(blank=True, null=True)
technician_name=models.CharField(max_length=50,blank=True, null=True)
terms_and_condition=models.TextField(max_length=250,blank=True, null=True)
thumb_impression=models.ImageField(_('cust_thumb'), upload_to='photos/')
cust_pic=models.ImageField(_('cust_image'), upload_to='photos/')
def __str__(self):
return self.cust_first_name
def image_tag_thumb(self):
return u'<img src="%s" width=100 height=120 />' % (self.thumb_impression.url)
image_tag_thumb.short_description = 'Customer Image'
image_tag_thumb.allow_tags = True
def image_tag(self):
return u'<img src="%s" width=100 height=120 />' % (self.cust_pic.url)
image_tag.short_description = 'Image'
image_tag.allow_tags = True
def save(self):
a=int(self.total_payment_received)
b=int(self.rent_fixed)
self.balance=b-a
super(Account,self).save()
if(self.cust_id==None):
print "inside cust id loop"
queryset=Account.objects.all()
data=request.POST
temp=queryset.aggregate(Max('cust_id'))
print temp
temp=temp.get('cust_id__max')
print "temp>>",temp
cust_id=""
if not temp :
print"before"
cust_id="CUST0001"
print "cust_id=",cust_id
else:
print"after"
print "m=",temp
prefix=temp[0:4]
print "prefix",prefix
suffix=temp[4:]
print "suffix",suffix
int_suffix=int(suffix)
print "prefix=",prefix,"suffix=",suffix,"int_suffix",int_suffix
if int_suffix<9 :
suffix="000"+str(int_suffix+1)
elif int_suffix>=9 and int_suffix<99:
suffix="00"+str(int_suffix+1)
elif int_suffix>=99 and int_suffix<999:
suffix="0"+str(int_suffix+1)
elif int_suffix>=999 and int_suffix<9999:
suffix=""+str(int_suffix+1)
cust_id=prefix+suffix
print "cust_id=",cust_id
account=Account()
account.cust_id=cust_id
account.cust_first_name=data.get('cust_first_name')
account.cust=data.get('cust_address')
account.is_active=data.get('is_active',default=False)
if account.save():
return Response(status=status.HTTP_201_CREATED)
else:
return Response( status=status.HTTP_400_BAD_REQUEST)
else:
pass
print "cust_id",self.cust_id
现在,当我启动此项目并添加新帐户时,会保存但帐户名称无法点击。如果再次创建其他帐户,则会替换最后一个帐户
答案 0 :(得分:1)
class AccountAdmin(ImportExportModelAdmin, admin.ModelAdmin):
resource_class = AccounttResource
list_display = ['cust_first_name',]
list_display_links = ('cust_first_name',)
readonly_fields = ('image_tag_thumb', 'image_tag', 'cust_id',)
答案 1 :(得分:0)
您无法创建具有相同主键(cust_id)的2个帐户,因此在创建新帐户时,请确保输入新的非空密钥。
如果您无法单击该条目,请确保您具有适当的权限,而不是使用阻止您编辑该模型的内容覆盖has_change_permission
。另外,请确保您没有将list_display_links
设置为None
,空列表或某些不在list_display
中的字段或模型中根本不存在的字段。您可以尝试将list_display_links
指定为将链接到编辑表单的字段列表。