This seems like something that should be rather simple, and something that should be a common thing to need, but the more I try and find an answer, the more confused I get.
I have an event model that is linked to multiple other models that need events, so I created a separate event model for it. Now, I want to create an event when I create a job model, but I am having trouble figuring out how to do it.
In my job field, I have a OneToOneField relationship to an event, and the only thing I need to decide at the time I make the event is the duration.
A simplified Job model might look like this:
customer = models.ForeignKey(Customer)
address = models.CharField(max_length=100, verbose_name="Job Address", null=False, blank=False)
city = models.CharField(max_length=100, verbose_name="City", null=True, blank=True)
state = models.CharField(max_length=2, verbose_name="State", null=True, blank=True)
zip = models.CharField(max_length=5, verbose_name="Zip Code", null=True, blank=True)
# Simply need to set duration
event = models.OneToOneField(Event, on_delete=models.CASCADE, null=False, blank=False)
TIME_LIST = (
title = models.CharField(max_length=255)
employee = models.ForeignKey(Employee, null=True, blank=True)
start_time = models.DateTimeField(null=True, blank=True)
end_time = models.DateTimeField(null=True, blank=True)
range = RecurrenceField(null=True, blank=True)
duration = models.IntegerField(choices=TIME_LIST, null=True, blank=True)
is_all_day = models.BooleanField(default=False, null=False, blank=False)
template_name = 'jobs/create.html'
success_url = '/schedule/add/'
form_class = JobCreateForm
return self.success_url + str(self.object.id)
model = Job
fields = [
You could create a second form for
Event and rendering it in your template as well. Then in your view you can save it first, creating an event, then save your
Job form and set it's related event to the one just created.
Alternatively (the way I'd probably do it), if you'd prefer a single form, you could add the duration field to your jobs form and override the form's
save function. An overridden save could look something like this
def save(self, commit=True): e = Event.objects.create(duration=self.cleaned_data.get('duration')) job = super().save(commit=False) job.event = e job.save() return job
Formsets are really for making multiple forms, so in your case as you are using a
OneToOneField (thereby only creating requiring a single form to create a single related object) it wouldn't make sense to use them.