开发者

Instanting ModelForm with where modelfields are overwritten

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 rephras开发者_Go百科e 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!


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜