2

Models

class A(SiteAwareModel):

    name = models.CharField(max_length = 255, blank = False)
    description = models.TextField(blank = True, null = True)

    class Meta:
        def key_count(self):
            return B.objects.count(self)

Admin

class AAdmin(admin.ModelAdmin):
    list_display = ('key_count')
    list_filter = ('key_count')

admin.site.register(A, AAdmin)

Problem

I have a model A which has its own fields (name, description). Further, model A calculates a key count by using model B (no foreign key relation).

I want to access the key_count in the admin panel. I can see a column in the admin meaning list_display works but when I try using it in list_filter it throws an error.

'AAdmin.list_filter1' refers to 'key_count' which does not refer to a Field.

End result

I want to make a filter which would show key_count > 10

What I have done(without success)

I have already tried doing it via managers with the help of THIS blog post

What I DO NOT want

I do not want to change my model by writing new migrations.

Zain Khan
  • 3,753
  • 3
  • 31
  • 54
  • This question is just for django 1.3? Whats the relation betwen A and B if there is "no foreign key relation"? Could you show the "B" fields? – bsm Dec 04 '12 at 12:54
  • I am using django 1.3.4 to be specific. No FK relation bw the 2 tables. And B contains some common field lets say name(CharField) and type(IntegerField) – Zain Khan Dec 05 '12 at 07:10

1 Answers1

0

You can try to create a custom field class for your purpose https://docs.djangoproject.com/en/dev/howto/custom-model-fields/

If you don't want to write a migration, let south do it ^^

http://south.aeracode.org/

Good luck!

Thorin Schiffer
  • 2,818
  • 4
  • 25
  • 34