Django Admin Multiple Models and Dropdown list

476 views
Skip to first unread message

mab.mo...@gmail.com

unread,
Sep 18, 2018, 11:18:51 AM9/18/18
to Django users
I have the following models and admin files and I would like to be able to show the text representation of the ExpenseCategory category field in the dropdown list on the Admin page. However, the dropdown list is displaying "ExpenseCategory Object(n)" instead of the actual text category such as "Utilities", "Office Supplies" etc. 

I need to insert the ExpenseCategory pk in the Expenses table but want a human readable drop down list to show the category text instead. 

Any guidance would be appreciated. Thanks in advance......
  

MAIN/MODELS>PY

class ExpenseCategory(models.Model):
    category = models.CharField(max_length=200)
    deductible = models.TextField(choices=BOOLEAN_CHOICE)

    def __int__(self):
       return self.category

class Expenses(models.Model):
    expense = models.CharField(max_length=200)
    category = models.ForeignKey(ExpenseCategory, on_delete=models.PROTECT)
    expense_amount = models.DecimalField(max_digits=8, decimal_places=2)
    expense_date = models.DateField(auto_now_add=False)
    expense_type = models.TextField(choices=SOURCE)
    description = models.CharField(max_length=200)
    source = models.CharField(max_length=200)

    def __int__(self):
       return self.expense
------------------------------------
MAIN/ADMIN.PY

class ExpenseCategoryAdmin(admin.ModelAdmin):
   list_display = ['category','deductible']
   list_filter = ['deductible']

admin.site.register(models.ExpenseCategory, ExpenseCategoryAdmin)

class ExpensesAdmin(admin.ModelAdmin):
   list_display = ['expense','expense_amount','expense_date','category','expense_type']
   list_filter = ['expense_date','category','expense_type']

admin.site.register(models.Expenses, ExpensesAdmin)



Julio Biason

unread,
Sep 18, 2018, 11:30:20 AM9/18/18
to django...@googlegroups.com
Hey Mab,

You probably need to replace you `__init__` to `__str__`. They are two different things.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b0d5e22e-2d20-4e11-ac22-00fb1eb9794f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Julio Biason, Sofware Engineer
AZION  |  Deliver. Accelerate. Protect.
Office: +55 51 3083 8101  |  Mobile: +55 51 99907 0554

mab.mo...@gmail.com

unread,
Sep 18, 2018, 11:56:50 AM9/18/18
to Django users
Hi Julio, 

Thank you so much. I should have picked that up myself. It worked!

Thanks again.

Marc
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages