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 9078211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:33:48+00:00 2026-06-16T19:33:48+00:00

I have a Django app where users submit orders for payment. Clearly, security is

  • 0

I have a Django app where users submit orders for payment. Clearly, security is important. I want to minimise the amount of code that I have to write, to avoid introducing any security holes, and ease maintenance.

The model is simple:

class Order(models.Model):
    user = models.ForeignKey(User)
    created = models.DateTimeField()
    paid = models.DateTimeField(null=True, blank=True)
    items = models.ManyToManyField(Item)

I’m using a CreateView to create instances of Order:

class OrderView(CreateView):
    model = Order
    form_class = OrderForm

I want to enforce values for certain fields in those instances. For example, I want the instance user field set to the current logged-in user. I don’t want any possibility that the user can change the value of this field, so I don’t want it to appear in the form at all. Therefore I use a custom ModelForm to remove these fields from the form:

class OrderForm(forms.ModelForm):
    class Meta:
        model = Order
        # For security, we control exactly which fields are placed
        # in the form, rather than excluding some:
        fields = ('items',)

Now I want the newly created Order instances to have the user field set to the current logged-in user. I can’t find any documentation about what is the best way to do this.

(A) I can override the form’s save() method to modify the object before saving, but it feels like this code doesn’t belong in the form, which doesn’t know anything about the user field. I also don’t have access to the request here, which I’d need to determine the current user. But it might look like this:

class OrderForm(forms.ModelForm):
    def save(self, commit=True):
        instance = super(OrderForm, self).save(commit=False)
        instance.user = get_request_magic().user
        if commit:
            instance.save()
        return instance

(B) I can override the view’s form_valid method to save the object with commit=False, like a class-based version of this question. But I can’t call the superclass method directly, because it saves the object with no way to disable commit, so I have to manually skip a generation of form_valid which is nasty. Apart from that complaint, this does look like the best way I’ve found so far:

class OrderView(CreateView):
    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.user = self.request.user
        self.object.save()
        return super(ModelFormMixin, self).form_valid(form)

(C) I could write a replacement for CreateView that adds a hook to allow objects to be changed before saving them. But that feels like more boilerplate and duplication.

(D) I can’t provide an initial value, because there’s no form field to put it in, so it will be ignored.

Any other ideas? If (B) the best option, is there any way around the hacky way of manually specifying which superclass’ form_valid method I want to call?

  • 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-16T19:33:49+00:00Added an answer on June 16, 2026 at 7:33 pm

    Django user Charettes answered the question for me:

    You can achieve this by overriding form_valid:

    class OrderCreateViewMixin(CreateView):
        def form_valid(self, form):
            form.instance.user = request.user
            return super(OrderCreateViewMixin, self).form_valid(form)
    

    Which pointed me towards the right part of the documentation:

    class AuthorCreate(CreateView):
        form_class = AuthorForm
        model = Author
    
        def form_valid(self, form):
            form.instance.created_by = self.request.user
            return super(AuthorCreate, self).form_valid(form)
    

    This is definitely the simplest and cleanest answer I’ve found so far. It doesn’t require modifying the form in any way, although it does directly access its instance member which is a bit ugly. However, at least it’s officially documented, so it’s unlikely to break.

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

Sidebar

Related Questions

I have a model in my dJango app called event . I want users
I'm looking for a simple Django app that restricts registration to users who have
I have a form in my django app where users can upload files. How
I have a Django app that gets it's data completely from an external source
I have a Django app that use a django-tagging. I need to port this
I have a django app that I made and have implemented a plist into
My situation is as follows. I have a django app that is a CMS,
I have a form in my Django app (not in admin) that allows staff
I have one problem with users menu. So, I want, that authenticated user can
Scenario I have a basic Django app in which users (django's authentication built in

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.