1

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',         
        )
Alasdair
  • 298,606
  • 55
  • 578
  • 516
cmanbst
  • 179
  • 3
  • 13

1 Answers1

1

You can override get_initial if you want to set initial data dynamically. If you don't need to set it dynamically you can simply set initial.

class JobCreation(LoginRequiredMixin, SuccessMessageMixin, generic.edit.CreateView):
    model = Job
    form_class = JobCreationForm

    def get_initial(self):
        initial = super().get_initial()
        initial['location'] = self.request.user.location
        return initial

    ...
Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • 1
    Works for me. I'm actually using a User model that just defines the user type and then extending that model to get BusinessUser (which is where I add details like city and state). So, I had to do something like: `user_info = BusinessUser.objects.get(pk=self.request.user.pk)` and then access the city and state fields from `user_info` with `initial['location'] = user_info.city + ", " + user_info.state`. I wouldn't think the standard use case needs this quibble so I'll leave the question as is if you agree. – cmanbst Apr 25 '18 at 16:52