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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:59:10+00:00 2026-05-17T23:59:10+00:00

I need set field value, not passed to Django Form constructor. I have model

  • 0

I need set field value, not passed to Django Form constructor.

I have model and form like this:

class Message(models.Model):
    created     = models.DateTimeField()
    text        = models.CharField(max_length=200, blank=True, null=True)
    active      = models.BooleanField(default=False)

class MessageForm(forms.ModelForm):
    class Meta:
        model   = Message
        exclude = ('created', 'active')

    def clean(self):
        # check if user is blocked
        if user.is_admin():
            self.cleaned_data['active'] = True
        return self.cleaned_data

Expected: if current user is admin – I need automatically set message as active. User should not pass this parameter by form.

Actual: I see that saved message always have flag “False” (I can delete condition and in this case I also see that message is not active).

Please help me understand, how can I do set this “active” flag in clean() method.

  • 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-05-17T23:59:10+00:00Added an answer on May 17, 2026 at 11:59 pm

    The previous answer would work, but I like encapsulating all the form’s internal operations like what to show and what not, within the form. I know you mentioned you don’t want to send a field value to the constructor, but if you don’t mind sending the user, your solution would work.
    i.e., your constructor:

    def __init__(self, user):
        self.user = user
        super(BaseForm, self).__init__()
    

    then in your clean, you just change the user to self.user.
    There is another added benefit to this. Say tomorrow you want to assign more fields based on your user, you don’t need to add anything to the views, you can simply add it to the form.

    EDIT:
    When you add a field to exclude, it is not available in the cleaned data. Instead, set its widget as hidden.

    active = forms.BooleanField(widget=forms.HiddenInput)
    

    EDIT 2: If you really don’t want the field in the form
    In this case, instead of overriding the clean, why don’t you override the save?

    def save (self):
        super(BaseForm, self).save()
        if user.is_admin():
            self.instance.active=True
        super(BaseForm, self).save()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a input tag which is non editable, but some times i need
I have a login.jsp page which contains a login form. Once logged in the
I have a snippet to create a 'Like' button for our news site: <iframe
I need to solve the following question which i can't get to work by
I need to develop a file indexing application in python and wanted to know
I am trying to load a html page through UIWebview.I need to disable all
I have several USB mass storage flash drives connected to a Ubuntu Linux computer
This is beyond both making sense and my control. That being said here is
We manage a site for a medical charity. They have a number of links
Is there a way to test if a collection is already initialized? try-catch only?

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.