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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:42:12+00:00 2026-05-21T11:42:12+00:00

I have a ModelForm field that is based on the following Model: class Phrase(models.Model):

  • 0

I have a ModelForm field that is based on the following Model:

class Phrase(models.Model):
   subject = models.ForeignKey(Entity) # Entity is unique on a per Entity.name basis
   object = models.ForeignKey(Entity) # Entity is unique on a per Entity.name basis

The modelform (PhraseForm) has a field ‘subject’ that is a CharField. I want users to be able to enter a string. When the modelform is saved, and the string does not match an existing Entity, a new Entity is created.

This is why I had to overwrite the “subject” field of the Modelform, as I cannot use the automatically generated “subject” field of the Modelform (I hope I’m making myself clear here).

Now, all tests run fine when creating a new Phrase through the modelform. But, when modifying a Phrase:

p = Phrase.objects.latest()
pf = PhraseForm({'subject': 'anewsubject'}, instance=p).

pf.is_valid() returns False. The error I get is that “object” cannot be None. This makes sense, as indeed, the object field was not filled in.

What would be the best way to handle this? I could of course check if an instance is provided in the init() function of the PhraseForm, and then assign the missing field values from the instance passed. This doesn’t feel as if it’s the right way though, so, is there a less cumbersome way of making sure the instance’s data is passed on through the ModelForm?

Now that I’m typing this, I guess there isn’t, as the underlying model fields are being overwritten, meaning the form field values need to be filled in again in order for everything to work fine. Which makes me rephrase my question: is the way I’ve handled allowing users to enter free text and linking this to either a new or existing Entity the correct way of doing this?

Thanks in advance!

  • 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-21T11:42:13+00:00Added an answer on May 21, 2026 at 11:42 am

    Why are you modifying using the form.

    p = Phrase.objects.latest()
    p.subject = Entity.objects.get_or_create(name='anewsubject')[0]
    

    docs for get_or_create

    If you are actually using the form it should work fine:

    def mod_phrase(request, phrase_id=None):
        phrase = get_object_or_404(Phrase, pk=phrase_id)
    
        if request.method == 'POST':
            form = PhraseForm(request.POST, instance=phrase)
            if form.is_valid():
                form.save()
                return HttpResponse("Success")
        else:
            form = PhraseForm(instance=phrase)
    
        context = { 'form': form }
    
        return render_to_response('modify-phrase.html', context,
            context_instance=RequestContext(request))
    

    Setting the instance for the ModelForm sets initial data, and also lets the form know which object the form is working with. The way you are trying to use the form, you are passing an invalid data dictionary (lacks object), which the form is correctly telling you isn’t valid. When you set the data to request.POST in the example above, the request.POST includes the initial data which allows the form to validate.

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

Sidebar

Related Questions

Say I have the following in my models.py : class Company(models.Model): name = ...
I have a this model: class Fleet(models.Model): company = models.ForeignKey(Company, editable=False) aircraft = models.ForeignKey(Aircraft)
I have a modelform which has one field that is a ForeignKey value to
I have a model (and model form based on it) that has several time
Let's say I have some contrived models: class Author(Model): name = CharField() class Book(Model):
I created a form class based on a model: class MyModel(models.Model): increasing_field = models.PositiveIntegerField()
I have a multiple ModelForm classes that each represent a different Model. I would
Have you managed to get Aptana Studio debugging to work? I tried following this,
I have a ManyToManyField that I want to present in a form, as a
I have a project that adds elements to an AutoCad drawing. I noticed that

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.