I feel Django docs are sparse and not detailed enough for me to see how to call the LogEntry properly. So I went looking on Google for examples on how to query my db and sort the output of the data according to when a LogEntry ‘CHANGE’ action takes place and then Django serves the template. There were lots of 10+ year old Stack Overflow questions and even those didn’t really answer the question I am looking for.
Here is one of the models I am working with now for my web app:
from django.contrib.admin.models import CHANGE, LogEntry
class ScriptSuggestion(LogEntry,models.Model):
# id = models.IntegerField(blank=False, null=False)
title = models.CharField(max_length=300,blank=True)
body = models.TextField(max_length=300000,blank=True)
author = models.CharField(max_length=300,blank=True)
slug = models.SlugField(unique=True,blank=True)
changed = LogEntry.objects.filter(action_flag=CHANGE,blank=False, null=False)
def __str__(self):
return f'{self.title}'
Notice the last declared attribute I’ve called: changed
. That is where I believe is the right place to call the LogEntry
model.
Here is my corresponding CBV:
class ContentListView(LoginRequiredMixin,ListView):
model = Content
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super(ContentListView, self).get_context_data(**kwargs)
# Add in a QuerySet of all the Baklawa
context['inductions'] = Induction.objects.all()
context['preambles'] = Preamble.objects.all()
context['research'] = Research.objects.all()
context['scriptsuggestions'] = ScriptSuggestion.objects.all().order_by('-changed')
context['stockscripts'] = StockScript.objects.all()
return context
Notice the context dictionary keyword argument showing: 'scriptsuggestions'
. I’ve appended .order_by('-changed')
to the end which is also my best attempt at putting the LogEntry’s model attribute reference in the right place.
There are now two major issues I’ve encountered with the above arrangements:
- When I created the db migrations initially, it prompted me to enter a default value. The message suggested entering
timezone.now
. I’ve seen this before so I went ahead and entered it. Big mistake. Afterwards if I recall correctly Django was saying that theid
needs to be an integer. Below was the Django traceback. I invoked the Django shell and tried to manually re-assign an integer value to theid
attribute for this class instance to replace the erroneous date data. But that didn’t work. Afterwards I addedblank=False, null=False
to theLogEntry
attribute inmodels.py
but it’s too late. Here was the error:
raise FieldError(
django.core.exceptions.FieldError: Local field 'id' in class 'ScriptSuggestion' clashes with field of the same name from base class 'LogEntry'.
So while I still need to resolve the issue outlined above, now I have got a new issue that needs to be resolved first:
- I am not sure what I did exactly to trigger it but now the Django dev server log is showing:
File "/home/<user>/dev/projects/python/2018-and-2020/hypno_juicer/venv/lib/python3.10/site-packages/django/apps/registry.py", line 143, in check_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
The most upvoted questions on Stack Overflow for this new second issue point to a misconfigured asgi.py
.
In another Django forum thread @jeff answered a similar question 3 years ago writing:
The Django doc referred to is for AppConfig methods. I’m not sure why this is a problem all of a sudden or what I did to cause this. Should I begin building an AppConfig Django app?