I have two models
class Document(Model):
    document_id = models.AutoField(primary_key=True)
    ... other fields
class DocumentMetaData(Model):
    documentMetaData_id = models.AutoField(primary_key=True)
    document_id = models.ForeignKey(Document, on_delete=models.CASCADE,
verbose_name="document file name")
    metadata = JSONField(blank=True)

The metadata in the DocumentMetaData model has a key 'Decade' in the
JSONField.

In the DocumentMetaDataAdmin I have this for a sortable Decade field:
    def get_decade(self, obj):
        return obj._decade
    get_decade.admin_order_field = '_decade'

I would like to have a get_decade function in the DocumentAdmin that
displays a Decade column in the DocumentAdmin page, and have that field
sortable. So far, I can get the Decade column in the DocumentAdmin by doing
this:
    def get_decade(self, obj):
        result =
DocumentMetaData.objects.get(document_id=obj.document_id).metadata['Decade']
        return result
    get_decade.admin_order_field = 'get_decade'

But the sorting part does not work - I get a field error as there is no
decade field in the Document model. Is there some way to do a reverse
lookup on the document_id field to the DocumentMetaData model (since
admin_order_field needs a related model) so I can sort on the decade field
in the DocumentAdmin?

Thanks!

Mark

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAEqej2MofanG2JPP1_-mQYBnDczXeFZpmt9r%3DWArWSUGd0n%2BBg%40mail.gmail.com.

Reply via email to