Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8914283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:40:42+00:00 2026-06-15T04:40:42+00:00

I have a model, which has an author ForeignKey , as such: class Appointment(models.Model):

  • 0

I have a model, which has an author ForeignKey, as such:

class Appointment(models.Model):
    # ...
    author = models.ForeignKey(User)

I want the author field to be set automatically when creating appointment to currently logged-in user. In other words, the author field should not appear in my Form class:

class AppointmentCreateForm(ModelForm):
    class Meta:
        model = Appointment
        exclude = ('author')

There are two problems:

  1. How to access the form in generic CreateView and set the author?
  2. How to tell the form to save the excluded field along with the values read from user input?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:40:43+00:00Added an answer on June 15, 2026 at 4:40 am

    I’ve modified my generic view subclass as such:

    class AppointmentCreateView(CreateView):        
        model=Appointment
        form_class = AppointmentCreateForm
    
        def post(self, request, *args, **kwargs):
            self.object = None
            form_class = self.get_form_class()
            form = self.get_form(form_class)
    
            # the actual modification of the form
            form.instance.author = request.user
    
            if form.is_valid():
                return self.form_valid(form)
            else:
                return self.form_invalid(form)
    

    There are few important parts here:

    • I modified form instance field, which holds the actual model that’s going to be saved.
    • You can of course get rid of the form_class
    • The post method that I’ve needed to modify was two classes above in hierarchy, so I needed to incorporate the base code and the self.object = None line, merging the overload and base into one function (I’m not calling super in post).

    I think it’s a good way to solve pretty common problem, and once again I don’t have to write my own custom view.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model which has a field status definde as: class Model(models.Model): ...
I have this models: class Comment(models.Model): text = models.TextField(max_length = 300) author = models.ForeignKey(User)
I have a Model which has some constants defined, like below: class Order(models.Model): WAITING
I have created a blog app which its model has an author field like
I have model Foo which has field bar. The bar field should be unique,
I have a model which has a certain behaviour implemented. class X { ....
Let's say I have some contrived models: class Author(Model): name = CharField() class Book(Model):
suppose I have this model: class PhotoAlbum(models.Model): title = models.CharField(max_length=128) author = models.CharField(max_length=128) class
I have the following model in Django: from django.db import models class User(models.Model): pen_name
I'm using ASP.NET MVC and have a model which has an image (byte array)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.