Cp Verma
Cp Verma

Reputation: 502

Entries in django admin panel is not clickable

enter image description here

I am making a django admin based project but due to this error i am unable to proceed.

my code is

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

now when i start this project and add new account.it is saved but account name is not clickable.and if again i create other account it replaces the last one

Upvotes: 0

Views: 1468

Answers (2)

GwynBleidD
GwynBleidD

Reputation: 20569

You can't create 2 accounts with identical primary key (cust_id), so on creating new one, make sure that you're entering new, non-empty key.

If you can't click that entry, make sure that you have proper permissions, you're not overwriting has_change_permission with something that will prevent you from editing that model. Also make sure that you're not setting list_display_links to None, empty list or some field that is not in list_display or simply does not exist in your model. You can try specifying list_display_links to list of fields that will link to edit form.

Upvotes: 0

EchoGlobal.tech
EchoGlobal.tech

Reputation: 704

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',)

Upvotes: 1

Related Questions