Hi, you need read this first
https://docs.djangoproject.com/en/4.0/ref/contrib/admin/actions/
and then this
https://stackoverflow.com/questions/3989894/create-a-django-admin-action-to-duplicate-a-record

Cheers


On Tue, Jan 11, 2022 at 3:44 AM skumar pandey <iamspandey02...@gmail.com>
wrote:

> Can you please create a user who can update their profile using django
> rest framework and along with jwt authentication its a request.
>
> On Tue, 11 Jan 2022, 12:24 pm Alibek Khojabekov, <
> alibek.khojabe...@nu.edu.kz> wrote:
>
>> Hello everyone,
>> I am bit confused about making duplicate of a Course object in admin
>> actions with all related child objects.  So my Models are:
>>
>> 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):
>> return self.name
>>
>> 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):
>> return self.name
>>
>> 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):
>> return self.name
>>
>> def get_absolute_url(self):
>> return reverse("sub_section", kwargs={"course_pk": self.section.course.pk
>> ,
>> "section_pk": self.section.pk, "sub_section_pk": self.pk})
>>
>> 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:
>> pk = obj.id
>> 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.pk = None
>> new_course.name = course.name + " Copy"
>> new_course.status = course.status
>>
>> # course_skills
>> for skill in course.skills.all():
>> new_skill = skill
>> new_skill.pk = None
>> 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.pk = None
>> new_section.course = new_course
>>
>> section_order_list = list()
>> new_section.save()
>> course_order_list.append(new_section.id)
>>
>> # section sub_section
>> for sub_section in section.sub_sections.all():
>> new_sub_section = sub_section
>> new_sub_section.pk = None
>> new_sub_section.section = new_section
>>
>> sub_section_order_list = []
>> new_sub_section.save()
>> section_order_list.append(new_sub_section.id)
>> # steps and text_link_file
>> for step in sub_section.steps.all():
>> new_step = step
>> new_step.pk = None
>> 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.pk = None
>> # new_test_step.step = new_step
>> # new_test_step.save()
>>
>> sub_section_order_list.append(new_step.id)
>> print(step.id) # error new step iz
>>
>> 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.pk = None
>> # 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")
>>
>> # tests = Test.objects.filter(sub_section__pk=sub_section.id)
>> # for test in list(tests):
>> # new_test = test
>> # new_test.pk = None
>> # 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.pk = None
>> # 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
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django REST framework" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-rest-framework+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-rest-framework/e411f03a-c6ee-4627-8bca-0ace14f1b231n%40googlegroups.com
>> <https://groups.google.com/d/msgid/django-rest-framework/e411f03a-c6ee-4627-8bca-0ace14f1b231n%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django REST framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-rest-framework+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-rest-framework/CABQ6D0yEbsX2%2BgLD16LkUUg6dZna2veZ7VEDw5crXhQfOT-MqA%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-rest-framework/CABQ6D0yEbsX2%2BgLD16LkUUg6dZna2veZ7VEDw5crXhQfOT-MqA%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>


-- 
att.
Carlos Rocha

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-rest-framework/CAM-7rO3J%3DZO%3DOSdni%3DrntBd6J0Tek_t3g9806vFcn0i_LNU8-g%40mail.gmail.com.

Reply via email to