Hi, I have a set of django apps, in which each one inherits from the previous. So, ForumComment (from the app forum) inherits from Comment (from the app posts_and_comments) etc.
ForumComment has a field ‘moderation’ which is a datetime field with null=true.
Comment has a datetime field ‘date_created’.
I have an admin setup that presents the opportunity to approve a comment(s) when it has been reported for moderation, by setting the queryset’s moderation to None, using queryset.update(moderation=None).
However, when the queryset.update function runs, I get the following error -
*** django.db.utils.OperationalError: (1054, "Unknown column 'django_posts_and_comments_comment.date_created' in 'order clause'")
It seems that the queryset returned by the admin panel to the approve_comment function (see below code) is only able to access the fields of the inherited class ForumComment, and not the superclass fields of Comment.
Worst of all, I am sure this used to work ok, but for some reason it has stopped. I have a ForumPost, that inherits from Post, that also is broken. I am guessing that the decorator of the admin class is not capable of producing the completely fully fleshed out model of all the fields of both the model and the superclass model.
django_forum_app.models
class ForumComment(Comment):
author: models.CharField = models.CharField(default='', max_length=40)
forum_post: models.ForeignKey = models.ForeignKey(
ForumPost, on_delete=models.CASCADE, related_name="forum_comments")
active: models.BooleanField = models.BooleanField(default='True')
moderation: models.DateField = models.DateField(null=True, default=None, blank=True)
title: models.SlugField = models.SlugField()
class Meta:
ordering = ['date_created']
permissions = [('approve_comment', 'Approve Comment')]
def save(self, force_insert=False, force_update=False, using=DEFAULT_DB_ALIAS, update_fields=None) -> None:
self.post = Post(self.forum_post)
super().save()
def get_absolute_url(self) -> str:
return self.forum_post.get_absolute_url() + '#' + self.title
def get_category_display(self) -> str:
return 'Comment'
django_posts_and_comments.models
class Comment(models.Models) #SoftDeletionModel):
"""
a post can have many comments
"""
text: models.TextField = models.TextField(max_length=500)
post: models.ForeignKey = models.ForeignKey(
Post, null=True, on_delete=models.SET_NULL, related_name="comments")
date_created: models.DateTimeField = models.DateTimeField(auto_now_add=True)
user_profile: models.ForeignKey = models.ForeignKey(
Profile, null=True, on_delete=models.SET_NULL, related_name="comments")
# def save(self, **kwargs) -> None:
# super().save(**kwargs)
def comment_author(self) -> str:
return self.user_profile.display_name
def __str__(self) -> str:
# TODO check for query - fetch_related...
return "Comment for " + f"{self.post.title}"
django_forum_app.admin
admin.register(ForumComment)
class ForumCommentAdmin(admin.ModelAdmin): # SoftDeletionAdmin):
#fields = ('moderation', 'active', 'author', 'title', 'text', 'date_created', 'deleted_at', 'user_profile')
# fieldsets = [
# ('Moderation', {'fields': ['moderation']}),
# ('Active', {'fields': ['active']}),
# ('Author', {'fields': ['author']}),
# ('Text', {'fields': ['text']}),
# ]
list_display = ('moderation', 'active', 'post_str',
'author', 'text', 'date_created') # 'deleted_at')
list_editable = ('text', )
list_filter = ('moderation', 'active', 'date_created',
'post', 'author') # 'deleted_at')
search_fields = ('author', 'text')
def post_str(self, obj: ForumComment) -> str:
link = reverse("admin:django_forum_app_forumpost_change",
args=[obj.forum_post.id])
return mark_safe(
f'<a href="{link}">{escape(obj.forum_post.__str__())}</a>')
post_str.short_description = 'ForumPost' # type: ignore
# make row sortable
post_str.admin_order_field = 'forumpost' # type: ignore
actions = ['approve_comment']
def approve_comment(self, request: HttpRequest, queryset: QuerySet):
breakpoint()
updated = queryset.update(moderation=None)
self.message_user(request,
ngettext(
'%d comment was approved.',
'%d comments were approved.',
updated,
) % updated,
messages.SUCCESS)