'How to add + buttons on the Many to many TabularInline fields
How can I add the "+" sign to add the Many to Many field , below is the picture which shows the TabularInline with the name : SUBTOPIC-COURSE RELATIONSHIPS.
What I want is to be able to add a subtopic via here directly on this page
This is how my code is admin.py:
class SubTopicInline(admin.StackedInline):
model = models.SubTopic.course.through
class CourseAdmin(admin.ModelAdmin):
inlines = [SubTopicInline]
admin.site.register(models.Course, CourseAdmin)
Then the models:
class Course(TimeStampedModel, models.Model):
"""
Course model responsible for all courses.
:cvar uid: UID.
:cvar title: Course title.
:cvar description: Description of the course.
:cvar course_cover: Image for the course.
:cvar category: Course Category.
:cvar user: Author of the course.
:cvar review: Course review.
"""
uuid = models.UUIDField(unique=True, max_length=500,
default=uuid.uuid4,
editable=False,
db_index=True, blank=False, null=False)
title = models.CharField(
_('Title'), max_length=100, null=False, blank=False)
user = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.title
class Meta:
verbose_name_plural = "Courses"
class SubTopic(TimeStampedModel, models.Model):
"""
Subtopic for the course.
"""
uuid = models.UUIDField(unique=True, max_length=500,
default=uuid.uuid4,
editable=False,
db_index=True, blank=False, null=False)
sub_topic_cover = models.ImageField(
_('Sub topic Cover'), upload_to='courses_images',
null=True, max_length=900)
title = models.CharField(
_('Title'), max_length=100, null=False, blank=False)
course = models.ManyToManyField(Course, related_name='sub_topic',
blank=True)
def __str__(self):
return self.title
class Meta:
verbose_name_plural = "Sub topic"
class Lesson(TimeStampedModel, models.Model):
"""
Lesson for the sub topic.
"""
uuid = models.UUIDField(unique=True, max_length=500,
default=uuid.uuid4,
editable=False,
db_index=True, blank=False, null=False)
subtopic = models.ManyToManyField(SubTopic, related_name='lessons',
blank=True)
video_link = models.CharField(
_('Video Link'), max_length=800, null=False, blank=False)
def __str__(self):
return self.title
class Meta:
verbose_name_plural = "Lesson"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

