Long story short, I am creating a ModelForm which will be passed to a generic CreateView. This form will allow a user to post an event with a location. I have already collected the user's base address in my user creation form, and my Event model has a foreignkey to the author of the event.
I would like to display the city and state of the currently logged in user as a default value on the event creation form. Since default values are set at the model level, I am not able to use the requests framework. Solution such as this one offer a way to save info from the request to the database upon submission but I would like to display this default when the user first navigates to the form page. How do I achieve this functionality?
Edit I would like to be able to pass an initial parameter as in this post but have it dynamically determined by the current logged in user.
Here is the essential part of the model. Note that BusinessUser has city and state fields.
class Job(models.Model):
author = models.ForeignKey(BusinessUser, on_delete = models.CASCADE)
location = models.CharField(max_length=200, blank=True, help_text="If different from the location listed on your company profile.")
And here is the view so far:
class JobCreation(LoginRequiredMixin, SuccessMessageMixin, generic.edit.CreateView):
model = Job
form_class = JobCreationForm
context_object_name = 'job'
success_url = reverse_lazy('jobs:business_profile')
success_message = 'New job posted!'
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
JobCreationForm is the work in progress that I'm stuck on. At the moment, it's a ModelForm giving the model and fields.
class JobCreationForm(ModelForm):
class Meta:
model = Job
fields = (
'job_title',
'location',
)