2017-05-09 1 views
-1

장고 모델 Customer을 가지고 있으며 사용자 정의 admin 조치를 작성하여 모델 필드 중 하나를 업데이트했습니다. 다음과 같이사용자 정의 Django 관리자 조치가 유효하지 않은 리터럴 오류를 반환합니다.

def age(ModelAdmin, request, queryset): 
if customer.Age == '60 +': 
    customer.Age = 0 
elif customer.Age == '36 - 59': 
    customer.Age = 1 
else: 
    customer.Age = 2 
return customer.Age 

def education(ModelAdmin, request, queryset): 
    if customer.Education == 'Highschool and below': 
     customer.Education = 0 
    else: 
     customer.Education = 1 
    return customer.Education 

def employment(ModelAdmin, request, queryset): 
    if customer.Employment == 'Student': 
     customer.Employment = 0 
    elif customer.Employment == 'Contract': 
     customer.Employment = 1 
    else: 
     customer.Employment = 2 
    return customer.Employment 

def stability(ModelAdmin, request, queryset): 
    if customer.Employer_Stability == 'Unstable': 
     customer.Employer_Stability = 0 
    else: 
     customer.Employer_Stability = 1 
    return customer.Employer_Stability 

def residential(ModelAdmin, request, queryset): 
    if customer.Residential_Status == 'Rented': 
     customer.Residential_Status = 0 
    else: 
     customer.Residential_Status = 1 
    return customer.Residential_Status 

def salary(ModelAdmin, request, queryset): 
    if customer.Salary <= 1000: 
     customer.Salary = 0 
    elif 1000 < customer.Salary <= 10001: 
     customer.Salary = 1 
    else: 
     customer.Salary = 2 
    return customer.Salary 

def loyalty(ModelAdmin, request, queryset): 
    if customer.Customer_Loyalty <= 2: 
     customer.Customer_Loyalty = 0 
    else: 
     customer.Customer_Loyalty = 1 
    return customer.Customer_Loyalty 

def balance(ModelAdmin, request, queryset): 
    if customer.Balance <= 2500: 
     customer.Balance = 0 
    elif 2500 < customer.Balance <= 10001: 
     customer.Balance = 1 
    else: 
     customer.Balance = 2 
    return customer.Balance 

def feat_list(age, education, employment, stability, residential, salary, 
     loyalty, balance): 
    total = age() + education() + employment() + stability() + residential() 
     + salary() + loyalty() + balance() 
    return total 

def allocate_service(ModelAdmin, request, queryset): 
    platinum_customers = [] 
    silver_customers = [] 
    gold_customers = [] 
    message = '' 

    for customer in queryset: 
     if feat_list() <= 11: 
      customer.Service_Level = Service.objects.get(service_name = 
       'Silver Package') 
      silver_customers.append(customer.Name) 
     elif feat_list() > 11 and feat_list() <= 15: 

      customer.Service_Level = Service.objects.get(service_name = 
       'Gold Package') 
      gold_customers.append(customer.Name) 
     else: 
      customer.Service_Level = Service.objects.get(service_name = 
       'Platinum Package') 
      platinum_customers.append(customer.Name) 
     customer.save() 

     if platinum_customers: 
      message = 'The following customers are now Platinum Customers: 
       {}'.format(', '.join(platinum_customers)) 
     if silver_customers: 
      message = 'The following customers are now Silver Customers: 
       {}'.format(', '.join(silver_customers)) 
     if gold_customers: 
      message = 'The following customers are now Gold Customers: 
       {}'.format(', '.join(gold_customers)) 
     if not platinum_customers and not silver_customers and not 
      gold_customers: 
      message = 'No customer changes made!' 
     ModelAdmin.message_user(request, message, level=SUCCESS) 
allocate_service.short_description = 'Allocate Service' 

CustomerAdmin은 다음과 같습니다 : 다음과 같이 작업을 만들려면 나는 기능을 만들어

@admin.register(models.Customer) 
class CustomerAdmin(admin.ModelAdmin): 
    icon = '<i class="material-icons">account_box</i>' 
    list_display = ('Customer_ID', 'Name', 'Gender', 'Nationality', 
     'Account_Type', 'Salary', 'Balance', 'Service_Level') 
    list_per_page = 20 
    list_filter = ('Nationality', 'Gender') 
    actions = [allocate_service] 

여기서 문제는 내가이 오류를 얻고 작업을 실행하려고하면

ValueError: invalid literal for int() with base 10: '4941" class="action-select' 

답변

0

정수와 문자열을 섞어서 모델 값을 덮어 쓰고 있습니다.

elif customer.Age == '36 - 59': 
    customer.Age = 1 

귀하의 모델은 문자열 '36 - 59' 및 정수 1을 대표 할 수 없다.

대신 당신은 예를 들어, 자신의 변수로이 값을 추적해야합니다 데이터베이스 스키마를 변경할 수있는 경우

def education(ModelAdmin, request, queryset): 
    education = 0 
    if customer.Education == 'Highschool and below': 
     education = 0 
    else: 
     education = 1 
    return education 

이, 마지막으로

def education(ModelAdmin, request, queryset): 
    if customer.Education == 'Highschool and below': 
     return 0 
    else: 
     return 1 

단축 할 수있다 이 값을 저장하기 위해 문자열 대신 choice fields을 사용하는 것이 좋습니다.

+0

의견에 감사드립니다. 귀하의 권고에 맞게 코드를 업데이트했지만 여전히 ValueError : 밑이 10 인 int()에 대한 리터럴이 잘못되었습니다 : '21 "class ="action-select " – maffsojah

관련 문제