In Django, I want to be able to bulk create items with a foreign key to an object within its admin form. How do I do this?

Course Queries Syllabus Queries 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Syllabus Queries related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

So I am working on a course syllabus app. Each syllabus has one or more class meeting times, a start date, an end date. I would like to be able to automatically generate a set of class meetings for each syllabus based on this information within the admin form. The idea is simply that the user should be able to input the meeting times, start date, and end date, hit a button, and have django create inline class session objects that the user can then edit. So, for instance, if I have a class that meets every Tuesday from 1 - 3 between May 1 and September 1, I should be able to hit a button, and get class sessions--or at least class session forms, there's no reason to save them in the database until they're filled in--for every Tuesday between those dates.

The relevant model code is as follows:

class Syllabus (models.Model):
    name = models.CharField(max_length=120)
    number = models.CharField(max_length=32, blank = True)  
    start_date = models.DateField()
    end_date = models.DateField()

class DaysTime(models.Model):
    days = MultiSelectField(max_length=15, choices=WEEKDAYS)
    startTime = models.TimeField()
    endTime = models.TimeField()

    class Meta:
        abstract = True

class ClassTime(DaysTime):
    key = models.ForeignKey('Syllabus')

class EventAbstract (models.Model):
    name = models.CharField(max_length=120)
    description = models.TextField(blank = True)
    startDateTime = models.DateTimeField()
    endDateTime = models.DateTimeField(blank = True)

    class Meta:
        abstract=True

class ClassSession(EventAbstract):
    cancelled = models.BooleanField(default = False)
    syllabus = models.ForeignKey('Syllabus')
    date = models.DateField()

Generating the dates and the ClassSessions for the dates isn't the problem. Adding a button onto a custom admin template to extend change_form isn't a problem either. Because the change_form is highly generic, and does not know anything about the models it is presenting, there's no way I can see to make the button talk to the function.

So as I see it the problem is three fold:

  1. How do I supply the recurring class times to the function that generates the new ClassSessionInlines?
  2. How do I create them on the admin page?
  3. How do I populate their fields?

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.