<commit_before>from django.contrib import admin
from .models import URL, URLRedirect, URLVisible


class RedirectInline(admin.StackedInline):
    model = URLRedirect
    extra = 0
    max_num = 1


class VisibleInline(admin.StackedInline):
    model = URLVisible
    extra = 0
    max_num = 1


class URLAdmin(admin.ModelAdmin):
    list_display = ['path', 'modified']
    list_display_links = ['path']
    actions = None
    search_fields = ['^path']
    ordering = ('-modified',)
    inlines = [VisibleInline, RedirectInline]
admin.site.register(URL, URLAdmin)
<commit_msg>figured out a way to ask for relationship names at runtime, doesn’t distinguish between select_related/prefetch_related though yet.<commit_after>from itertools import chain
from django.contrib import admin
from .models import URL, URLRedirect, URLVisible


class RedirectInline(admin.StackedInline):
    model = URLRedirect
    extra = 0
    max_num = 1


class VisibleInline(admin.StackedInline):
    model = URLVisible
    extra = 0
    max_num = 1


class URLAdmin(admin.ModelAdmin):
    list_display = ['path', 'modified']
    list_display_links = ['path']
    actions = None
    search_fields = ['^path']
    ordering = ('-modified',)
    inlines = [VisibleInline, RedirectInline]

    def get_runtime_relations(self):
        """
        For the middleware, of all things.
        Because this is the best place to find out wtf we might want to
        ask for at runtime without configuration.
        """
        models = (x.model for x in self.inlines[:] if hasattr(x, 'model'))
        fields = (x._meta.get_fields_with_model() for x in models)
        flat_fields = chain(*fields)
        just_fields = (x[0] for x in flat_fields)
        only_rels = (x for x in just_fields
                     if hasattr(x, 'rel'))
        only_good_rels = (x for x in only_rels if x.rel is not None)
        only_ours = (x for x in only_good_rels if x.rel.to == URL)
        accessors = (x.related.get_accessor_name() for x in only_ours
                     if hasattr(x, 'related'))
        return accessors

admin.site.register(URL, URLAdmin)

{'fraction_non_alphanumeric': 0.09057639524245197, 'fraction_numerical': 0.004117108874656907, 'mean_word_length': 2.645, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7011543', 'n_tokens_mistral': 692, 'n_tokens_neox': 639, 'n_words': 199}