Framework/Django
Django 장고 admin 어드민 method 메소드 unknown field Error
청렴결백한 만능 재주꾼
2021. 4. 21. 23:22
반응형
#course/models.py
class AccountCoursePeriod(models.Model):
account = models.ForeignKey('member.Account', on_delete=models.SET_NULL, null=True)
course_period = models.ForeignKey('CoursePeriod', on_delete=models.SET_NULL, null=True)
weekly_commit = models.CharField(max_length=10)
is_paid = models.BooleanField(default=False)
def progress_check(self):
cohort_start = self.course_period.period.start_date
today = datetime.date.today()
gap_week = math.floor(( cohort_start - today ).days / 7)
week_in_this_course = self.course_period.course.week_set.all()
if self.account:
if gap_week < week_in_this_course.count():
is_done = week_in_this_course[gap_week].lecture_set.first().lectureaccount_set.first().is_done
if is_done:
return True
else:
return False
else :
is_done = week_in_this_course.last().lecture_set.first().lectureaccount_set.first().is_done
if is_done:
return True
else:
return False
else:
return " "
progress_check.short_description = 'Progress Check'
class Meta:
db_table = 'accounts_courses_period'
ordering = ('account','course_period')
verbose_name = 'Member & Cohort'
def __str__(self):
return self.course_period.course.name + str(self.course_period.cohort) + ' : ' + self.account.first_name + ' ' + self.account.last_name
#course/admin.py
class AccountCoursePeriodInline(admin.TabularInline):
model = AccountCoursePeriod
fields = ['account','weekly_commit','progress_check',]
class CoursePeriodAdmin(admin.ModelAdmin):
list_display = ['__str__','period_display', 'tuition', 'total_display']
inlines = [AccountCoursePeriodInline]
admin에서 admin.tabularInline을 사용하여 inline에 넣으려고 한 나 자신. 분명 메소드로 지정을 해서 인식이 되어야 함에도 불구하고 계속 에러를 맞았고 수많은 검색 끝에 결국 찾은 것 .. 아주 간단한 ..
class AccountCoursePeriodInline(admin.TabularInline):
model = AccountCoursePeriod
readonly_fields = ['account','weekly_commit','progress_check',]
readonly_fields 로 해주면 간단하게 된다. 실제로 fields로 넣으면 거기서 바로 수정이 가능하다. 하지만 inline으로 들어가고 정보만 보여주기를 원한다면 readonly_fields를 쓰고 메소드도 같이 활용할 수 있다. 하루하고 반나절 날렸다.
반응형