class Course(models.Model):
PROGRAMMING = 1
DESIGN = 2
ROBOTICS = 3
DIGITAL_LITERACY = 4
IT_ENTREPRENEURSHIP = 5
CATEGORY_CHOICES = (
(PROGRAMMING, 'Программирование, IT предпринимательство'),
(DESIGN, 'Компьютерная графика и медиа'),
(ROBOTICS, 'Робототехника и электроника'),
(DIGITAL_LITERACY, 'Компьютерная грамотность'),
)
NEW = 1
PROGRESS = 2
READY = 3
STATUS_CHOICES = (
(NEW, 'Новый'),
(PROGRESS, 'В прогрессе'),
(READY, 'Готово'),
)
class Meta:
ordering = ['pk']
course_type = models.ForeignKey(
Category, related_name='courses', on_delete=models.SET_NULL, blank=True, null=True, db_index=True)
name = models.CharField(max_length=120, db_index=True)
short_description = models.TextField(blank=True, null=True)
description = models.TextField(blank=True, null=True)
image = models.FileField(upload_to=upload_path, null=True, blank=True)
icon = models.FileField(upload_to=upload_path, null=True, blank=True)
video_link = models.URLField(max_length=200, null=True, blank=True)
target_audience = models.TextField(blank=True, null=True)
price = models.DecimalField(max_digits=8, decimal_places=2, default=0.00, validators=[
MinValueValidator(Decimal('0.00'))], blank=True)
author = models.ForeignKey(
'users.User', default=1, on_delete=models.SET_NULL, null=True)
language = models.CharField(
max_length=3, choices=settings.LANGUAGES, default='ru', db_index=True)
category = models.PositiveSmallIntegerField(
choices=CATEGORY_CHOICES, default=PROGRAMMING, db_index=True)
robotics_kit = models.ForeignKey(
RoboticsKit, related_name='courses', on_delete=models.SET_NULL, blank=True, null=True)
course_class = models.ManyToManyField(
CourseClass, related_name='courses')
status = models.PositiveSmallIntegerField(
choices=STATUS_CHOICES, default=NEW, blank=True)
order_list = models.CharField(max_length=500, validators=[
int_list_validator], blank=True, null=True)
created_date = models.DateTimeField(auto_now_add=True)
is_accessible = models.BooleanField(default=True, null=True, blank=True)
is_intro = models.BooleanField(default=False, null=True, blank=True)
projects = models.ManyToManyField(
"self", blank=True, related_name='courses')
# objects = DefaultSelectOrPrefetchManager(
# select_related=('robotics_kit', 'course_class', 'course_type'))
def __str__(self):
class Section(models.Model):
class Meta:
ordering = ['pk']
course = models.ForeignKey(
Course, related_name='sections', on_delete=models.CASCADE)
name = models.CharField(max_length=120)
created_date = models.DateTimeField(auto_now_add=True)
order_list = models.CharField(max_length=500, validators=[
int_list_validator], blank=True, null=True)
def __str__(self):
class SubSection(models.Model):
class Meta:
ordering = ['pk']
section = models.ForeignKey(
Section, related_name='sub_sections', on_delete=models.CASCADE)
name = models.CharField(max_length=120)
goal = models.TextField(max_length=500, default='', blank=True, null=True)
image = models.FileField(upload_to=upload_path, null=True, blank=True, validators=[
FileExtensionValidator(allowed_extensions=['jpg', 'png', 'jpeg'])])
link = models.URLField(max_length=200, null=True, blank=True)
created_date = models.DateTimeField(auto_now_add=True)
order_list = models.CharField(max_length=500, validators=[
int_list_validator], blank=True, null=True)
is_homework = models.BooleanField(default=False)
materials = GenericRelation(
"Material", null=True, related_query_name='sub_section')
comments = GenericRelation(
"Comment", null=True, related_query_name='sub_sections')
point = models.PositiveSmallIntegerField(default=0)
skills = models.ManyToManyField(Skill)
hours = models.PositiveIntegerField(default=0, blank=True)
def __str__(self):
def get_absolute_url(self):
class Step(models.Model):
class Meta:
ordering = ['pk']
TEXT = 1
HINT = 2
TEST = 3
EXERCISE = 4
CODE_EXAMPLE = 5
MODELING_3D = 6
VIDEO = 7
MATERIALS = 8
TYPE_CHOICES = (
(TEXT, 'Текст'),
(HINT, 'Подсказка'),
(TEST, 'Тестовое задание'),
(EXERCISE, 'Упражнение для программирование'),
(CODE_EXAMPLE, 'Пример кода'),
(MODELING_3D, 'Пример 3D модели'),
(VIDEO, 'Видео'),
(MATERIALS, 'Материалы'),
)
sub_section = models.ForeignKey(
SubSection, related_name='steps', on_delete=models.CASCADE)
name = models.CharField(blank=True, max_length=255)
step_type = models.PositiveSmallIntegerField(
choices=TYPE_CHOICES, default=TEXT)
materials = GenericRelation(
"Material", null=True, related_query_name='step')
created_date = models.DateTimeField(auto_now_add=True)
class TextLinkFileStep(models.Model):
step = models.OneToOneField(
Step, related_name='step_text_link_file', on_delete=models.CASCADE, null=True, blank=True)
text = models.TextField(blank=True, null=True)
file = models.FileField(upload_to=upload_path, null=True, blank=True)
link = models.URLField(max_length=200, null=True, blank=True)
created_date = models.DateTimeField(auto_now_add=True)
so here I need help how to correctly get those fileds and make duplicate from top Course object to bottom TextLinkFileStep objects
def make_duplicate(modeladmin, request, queryset):
for obj in queryset:
try:
course = Course.objects.prefetch_related(
"sections",
"sections__sub_sections",
"sections__sub_sections__tests",
"sections__sub_sections__tests__answers",
Prefetch(
"sections__sub_sections__steps",
Step.objects.select_related("step_text_link_file", "step_test"),
),
"skills",
).get(pk=pk)
# new course
new_course = course
new_course.status = course.status
# course_skills
for skill in course.skills.all():
new_skill = skill
new_skill.course = new_course
new_skill.save()
course_order_list = list()
# changing order list
new_course.save()
# course_sections
for section in course.sections.all():
new_section = section
new_section.course = new_course
section_order_list = list()
new_section.save()
# section sub_section
for sub_section in section.sub_sections.all():
new_sub_section = sub_section
new_sub_section.section = new_section
sub_section_order_list = []
new_sub_section.save()
# steps and text_link_file
for step in sub_section.steps.all():
new_step = step
new_step.sub_section = new_sub_section
new_step.save()
# copying test_step and test_step
if step.step_type == Step.TEST:
print("step is test")
# new_test_step = step.step_test
# new_test_step.step = new_step
# new_test_step.save()
try:
# text_file_link, txt_file_created = TextLinkFileStep.objects.get_or_create(
# step=Step(pk=step.id)) # .values('text', 'file', 'link') # new_text_file_link = text_file_link
# needed line below
new_text_file_link = TextLinkFileStep.objects.create(
step=step,
text=step.text_file_link.text,
link=step.text_file_link.link,
file=step.text_file_link.file,
)
# new_text_file_link = step.step_text_link_file
# new_text_file_link.step = new_step
# new_text_file_link.text = text_file_link.text
print("text is", step.text_file_link)
new_text_file_link.save()
except:
print("text link file step tries to be existed")
# for test in list(tests):
# new_test = test
# new_test.sub_section = new_sub_section
# new_test.save()
# answers = AnswerStep.objects.filter(test__pk=test.id) # for answer in list(answers):
# new_answer = answer
# new_answer.test = new_test
# new_answer.save()
new_sub_section.order_list = from_list_to_chars(sub_section_order_list)
new_sub_section.save()
new_section.order_list = from_list_to_chars(section_order_list)
new_section.save()
new_course.order_list = from_list_to_chars(course_order_list)
new_course.save()
except Course.DoesNotExist:
print("course does not exist")
So my function above is failing in Step and TextLinkFileStep objects
I need your assistance in this issue. My only hope is for this community))
Feel free to make any suggestions
Best regards