I have 2 models, Question model is associated with Group model using Foreign key.
class Question(models.Model):
comment = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
group = models.ForeignKey(
'Group', on_delete=models.CASCADE, related_name='questions')
question = models.FileField(
upload_to=user_directory_path, blank=True, null=True)
def __str__(self):
return self.comment
class Group(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
Now, inside admin, at some url- for example- http://127.0.0.1:8000/admin/questions/<group-id>
I want to show all questions related to a group of particular ID and clicking on each question, open a particular question (like a normal model object is open).
Note- I want to use Django template.
What I found on googling(but I have tried, I didn’t reach to a solution with it)- [get_urls] (https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_urls)
Following, I am using a temporary solution (but the problem with it, is the url admin/question/question/?group__id__exact=3
) basically, I am feeding a url to group name, and clicking on it ultimarlty load different result filtering as required.
class GroupAdmin(admin.ModelAdmin):
class Meta:
model = Group
list_display = ['group_wise_questions']
def group_wise_questions(self, obj):
redirect_url = reverse('admin:question_question_changelist')
redirect_url1 = reverse('admin:{}_{}_changelist'.format(
obj._meta.app_label, obj._meta.model_name)
)
extra = "?group__id__exact=%d" % (obj.id)
return format_html(
"<a href='%s'>All Questions from group (%s)</a>" % (
(redirect_url + extra), obj.id)
)
But I want something like http://127.0.0.1:8000/admin/questions/<group-id>
.
Any help/guidance will be helpful. Thanks in advance.