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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:47:41+00:00 2026-05-27T13:47:41+00:00

This may be a dumb question, but I’m a bit unsure if it’s safe

  • 0

This may be a dumb question, but I’m a bit unsure if it’s safe to manually set the cleaned_data. The docs says:

Once is_valid() returns True, you can process the form submission
safe in the knowledge that it conforms to the validation rules defined
by your form. While you could access request.POST directly at this
point, it is better to access form.cleaned_data. This data has not
only been validated but will also be converted in to the relevant
Python types for you.

For more context, say we have a modelform which has several fields such as a book’s title, book’s author, and a field which asks for a url.

The form conditions are: if the url field is empty, the user must provide the title and author. If the url field is given and nothing else, I would parse the html from the given url and extract the title and author automatically for the user.

In the case where I automatically grab the title and author from the url, what would be the best way to handle saving this data to the model, since the form would return an empty cleaned_data for author and title? I made sure the data parsed will conform to the validate rules I have in the model, but setting cleaned_data like this seems suspicious.

In modelform class:

def save(self, commit = True, *args, **kwargs):
    parsed_title = ... # String returned by my html parsing function
    parsed_author = ... # String returned by my html parsing function
    self.cleaned_data['title'] = parsed_title
    self.cleaned_data['author'] = parsed_author

EDIT:

Thanks, I made it like so:

def save(self, commit=True, *args, **kwargs):
     instance = super(BookInfoForm, self).save(commit=commit, *args, **kwargs)
     ....
     instance.title = parsed_title
     instance.author = parsed_author

     return instance

This is a bit off topic since you’ve already answered the original question, but the above code breaks some other part. Instead of saving the compiled info to http://..../media/books/<id> where <id> is the book id, it saves it to http://..../media/books/None.

I have a add/edit function in my views.py that handles adding and editing:

def insert_or_modify(request, id=None):
    if id is not None:
         book = BookModel.objects.get(pk=id)
    else:
         book = BookModel()

   if request.method == 'POST':
       form = BookInfoForm(request.POST, instance=book)
       if form.is_valid():
           form.save()

   ....
   
   return render_to_response(...)

Is there a way to make sure the id is present so that I won’t get id=None? I guess more specifically, in the save() in the modelform, is there a way to create a new instance with an id if instance.id = None? Although I thought calling super(ModelForm, self).save(…) would do that for me?

Thanks again!

  • 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-27T13:47:42+00:00Added an answer on May 27, 2026 at 1:47 pm

    In the case you present, your intention isn’t setting the cleaned_data, but the model data. Therefore, instead of setting cleaned_data in the save method, just set the attributes of self.instance and then save it.

    About setting cleaned_data manually, I don’t think it’s necessarily wrong, it may make sense to do it in the form’s clean method for some cross-field validation, although it’s not a common case.

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

Sidebar

Related Questions

This may sound like a bit of a dumb question but how do I
Ok, this may be a dumb question but here goes. I noticed something the
I know this may be a dumb question, but my background is more in
Okay, this may be a dumb question, but I've not been able to find
This may seem like a dumb question, but can an app build with c#
This may be a very dumb question but I can't seem to get it
this may be a dumb question, but I can't seem to find the answer.
I feel like this may be a dumb question, but it's late and my
This may be a completely dumb question, but I've seen a couple examples declaring
This may be a dumb question but it seems like activateIgnoringOtherApps: may be the

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.