Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

django - Not working Import tool and template override at the same time for same model

Trying to use both import tool and Django admin template override on the same model. However, it not working same time . Is there a particular solution for this? Codes are below...

from django.contrib import admin
from loanapi.models import SellGoldApplication, LoanApplication, 
from django.db import connections
from gloan.local_settings import vendor_db, api_url, LOCAL_ENV, SEND_TO, SEND_CC
from django import forms
from rangefilter.filter import DateRangeFilter
from django_admin_listfilter_dropdown.filters import (
    ChoiceDropdownFilter,
)
from django.contrib import messages
from .actions import export_as_csv_action, export_as_SOG_csv_action
import requests, json
from import_export.admin import ImportExportModelAdmin, ImportMixin
from import_export import resources, fields

from .model_config import LEAD_SOURCE_CHOICE
from django.forms import ValidationError
import requests, xlsxwriter, json, datetime, urllib
from django.core.mail import EmailMessage
from django.utils.html import mark_safe


class LoanApplicationForm( forms.ModelForm ):
    comments = forms.CharField( widget=forms.Textarea(attrs={'rows': 4, 'cols': 60}),required=False)

    def init(self , *args , **kwargs):
        super (LoanApplicationForm , self).init (*args , **kwargs)
        self.fields[ 'phone' ].required = True
        self.fields[ 'pincode' ].required = False
        self.fields[ 'processing_fee' ].initial = 1
        self.fields[ 'processing_fee' ].widget.attrs[ 'readonly' ] = True

    class Meta:
        model = LoanApplication
        fields = ('__all__')

class LoanApplicationResource(resources.ModelResource):
    # source = fields.Field(attribute='source', column_name=('source'))
    class Meta:
        model = LoanApplication
        # import_id_fields = ('source',)

    def before_import_row(self, row, **kwargs):
        source = row['source']
        try:
            source_choice = {value: key for key, value in LEAD_SOURCE_CHOICE}
            row['source'] = source_choice[source]
        except:
            raise ValidationError('Source is wrong please correct and import again. '
                                  'Error in row with value = %s' % row['source'])
            # row['source'] = 3
    # def dehydrate_source(self,LoanApplication):
    #     return '%s' % (LEAD_SOURCE_CHOICE[source])
# class LoanApplicationImport(ImportMixin, admin.ModelAdmin):
#     resource_class = LoanApplicationResource
#     pass

class LoanApplicationAdmin(admin.ModelAdmin, ImportMixin):
    show_delete = False
    form = LoanApplicationForm
    resource_class = LoanApplicationResource
    list_per_page = 50 # No of records per page
    list_display = ("phone", "name", "city", "pincode", "requested_amount", "vendor", "bank", "created_at",
                    "source", "status", "lead_sent","Remarks")
    list_editable = ('source','status',)
    list_filter = (('status', ChoiceDropdownFilter),('source',ChoiceDropdownFilter),"bank",('created_at', DateRangeFilter))
    # list_filter = (
    #     ('bank', admin.RelatedOnlyFieldListFilter),
    # )
    search_fields = ["phone","requested_amount","pincode","name"]
    fieldsets = (
        ('Customer' , {
            'fields': (('phone',"name","email"),("pincode","city"), ("requested_amount","vendor"))
        }) ,
        ('Details' , {
            'fields': (('bank','processing_fee'),('gold_required','interest_rate'),('comments','status'),
                       ('created_at', 'source'), 'whatsapp', 'sm_details', 'lead_sent')
        }) ,
    )

    readonly_fields = ('vendor','created_at','whatsapp','sm_details')
    actions = [export_as_csv_action()]

    def vendor(self , obj):
        vendor_name = "-"
        if obj.vendor_id:
            cursor = connections[ 'vendor' ].cursor ()
            query_str = '''SELECT vendor_name FROM ''' + vendor_db + '''.udropship_vendor WHERE vendor_id="''' + str (obj.vendor_id) + '''"'''
            exists = cursor.execute (query_str)
            if exists:
                vendor_name = cursor.fetchone ()[ 0 ]
            cursor.close()

        return vendor_name

    def Remarks(self, obj):
        return obj.comments
    Remarks.short_description = 'Comments and customer feedback'

    

class ImportAdmin( admin.ModelAdmin):
    change_list_template = 'admin/loanapi/BankLoanDetails/change_list.html'
admin.site.register(LoanApplication, LoanApplicationAdmin)

Getting no error but both are not reflecting. If any one option closed then other is working fine. Waiting for someones reply. Thanks in advance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.7k users

...