0

Now i have a situation in separate models choices and display in admin with different url

here it's my models action:

class LogAction(Enum):

BUY = "buy" 
REFUND = "refund" 
SEND = "send" 
RECEIVE = "receive" 
WITHDRAW = "withdraw" 
SUBSCRIPTION = "subscription" 
NONE = "none"

my models:

class TransactionLog(models.Model):
    ...
    
    action = models.CharField(max_length=32, choices=[(str(tag), tag.value) for tag in 
    LogAction],default=str(LogAction.NONE), db_index=True)
    
    ...

admin:

class TransactionLogAdmin(admin.ModelAdmin):
    list_display = (
        'get_event_type', 'user', 'target_user', 'action', 'created_at','get_real_point','success', 'note')

    def get_event_type(self, instance: TransactionLog):
        
        if instance.action == str(LogAction.SUBSCRIPTION):
            return "SUBSCRIPTION"
        elif instance.action == str(LogAction.WITHDRAW):
            return "WITHDRAW"
        elif instance.action == str(LogAction.REFUND):
            return "REFUND"
        elif instance.action == str(LogAction.BUY):
            return "BUY"
        else:
            return instance.action

    get_event_type.short_description = 'bill type'

here it's my backend admin. i'd like to show all 'buy' type in one table but different urls. and all 'refund','withdraw','subscription' is as same as 'buy' type so as to let me toggle to different pages. the multiple admin site is the option but is not suitable for this situation. what django tool can i use? plz give me some hint or some suggestion thanks a lot

enter image description here

Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39
  • Try using Django's `model.TextChoices` for your enum, https://docs.djangoproject.com/en/4.0/ref/models/fields/#enumeration-types and `ModelAdmin.list_filter` to filter by log action. https://docs.djangoproject.com/en/dev/ref/contrib/admin/filters/#using-a-field-name – Håken Lid May 19 '22 at 20:56
  • thank for your suggestion. i'v solved this problem. https://stackoverflow.com/questions/2223375/multiple-modeladmins-views-for-same-model-in-django-admin and https://stackoverflow.com/questions/851636/default-filter-in-django-admin is the best answer for me. – 林秩宇治與 May 20 '22 at 03:59

0 Answers0