2

I have set up a Django Admin Interface (django.contrib.admin) to see my data from my Mysql Database.

I would like to know if it's possible to make specifics SQL queries ?

So far, all my tables are registered to be displayee in the Django Admin Dashboard by doing for example :

# Register your models here.
@register(User)
class User(ModelAdmin):
    list_display = ('name', 'city')
    list_filter = ('name',)
    search_fields = ('name',)

@register(Object)
class Object(ModelAdmin):
    list_display = ('name', 'object_description', 'owner', 'acquired_date')
    list_filter = ('name',)

Let's say, I would like to display a specific table in the dashboard showing all the users with the appropriate object and the date of acquisition. How could I do that ? Or, better, how can I display data from multiple JOIN query ?

I saw the querySet function but I did not see any examples to do this kind of things.

Thank for your help

lilouch
  • 1,054
  • 4
  • 23
  • 43

1 Answers1

1

if owner is User foreign key, you can add users data in your Object admin list display like this:

@register(Object)
class Object(ModelAdmin):
    list_display = ('name', 'object_description', 'owner_name', 'owner_city', 'acquired_date')
    list_filter = ('name',)

    def owner_name(self, obj):
        return obj.owner.name
    owner_name.short_description = 'owner_name'

    def owner_city(self, obj):
        return obj.owner.city
    owner_city.short_description = 'owner_city'
weAreStarsDust
  • 2,634
  • 3
  • 10
  • 22