'Align models with TabularInline django admin with models
How best can I align well my models which are in a reverse relationship with the help of TabularInline, I tried the following below but failed :
class SubTopicInline(admin.TabularInline):
model = models.SubTopic
class CourseAdmin(admin.ModelAdmin):
inlines = [SubTopicInline]
admin.site.register(models.Course, CourseAdmin)
This is the error thrown :
ERRORS: <class 'courses.admin.SubTopicInline'>: (admin.E202) 'courses.SubTopic' has no ForeignKey to 'courses.Course'.
Then below is my 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 |
|---|
