Django

Code

Ticket #3257 (closed: fixed)

Opened 2 years ago

Last modified 2 years ago

Change form_for_model() to return correct field for ForeignKeys and ManyToManyFields

Reported by: Honza Král <Honza.Kral@gmail.com> Assigned to: adrian
Milestone: Component: Forms
Version: Keywords:
Cc: Honza.Kral@gmail.com, floguy@gmail.com, gandalf@owca.info, kilian.cavalotti@lip6.fr, yatiohi@ideopolis.gr Triage Stage: Accepted
Has patch: 1 Needs documentation: 1
Needs tests: 0 Patch needs improvement: 0

Description

Its a standard ChoiceField? with the added value that clean() returns instance of a model with the pk equals to value - designed to work with form_for_model... it takes model class as a first argument to __init__, ForeignKey.formfield() etc. has been updated to use it...

all tests pass

Attachments

related_choice_fields_moved_trunk_resync.patch (7.6 kB) - added by kilian.cavalotti@lip6.fr on 01/30/07 13:06:51.
Resynch'ed patch against trunk rev [4451]
related_choice_field_empty_label.patch (7.8 kB) - added by Honza Král <Honza.Kral@gmail.com> on 01/31/07 10:09:07.
added empty_label kwarg to the field, all tests pass now

Change History

01/10/07 04:36:51 changed by Honza Král <Honza.Kral@gmail.com>

if anybody feels that this is too tightly bound to django models (which it is) to be integral part of newforms, feel free to move it to some other location (db.models.formfields ?? ) in order to keep the decoupling...

01/17/07 17:27:10 changed by Honza Král <Honza.Kral@gmail.com>

  • stage changed from Unreviewed to Design decision needed.

01/17/07 17:51:34 changed by mir@noris.de

  • needs_better_patch set to 1.
  • needs_tests set to 1.
  • needs_docs set to 1.

Hi Honza--thanks for your patch! But, please don't put spaces around parentheses like in return self.model._default_manager.filter( pk__in=new_value ). There is a brief spec for the usual coding style at http://www.djangoproject.com/documentation/contributing/

01/17/07 23:58:00 changed by floguy@gmail.com

I have started working on tests for this patch, however I do have a few questions after writing most of the tests for the two new Fields:

  1. Currently, even if the value passed to the clean function is in the choices list, but is not a pk for the model, it will raise an error. Is this expected behavior? Do we want some sort of check on field construction to check that all of the choices are in fact pks for the given model?
  1. Setting required=False in either of the new fields does nothing, because when the clean function is called, if it doesn't find a result for that value, it will raise an exception. Should required=False be an option on these fields, or should the code be changed to allow for it?
  1. The section of the patch regarding django/newforms/models.py is no longer needed as far as I can tell.

Other than those few fairly minor questions, test creation is going quite well.

(follow-up: ↓ 6 ) 01/18/07 01:44:08 changed by floguy@gmail.com

  • cc changed from Honza.Kral@gmail.com to Honza.Kral@gmail.com, floguy@gmail.com.

(in reply to: ↑ 5 ) 01/18/07 08:45:32 changed by Honza Kral <honza.kral@gmail.com>

Replying to floguy@gmail.com:

  1. i thoguht that since it's meant to return model,a failure to do so (non-existent pk) is error
  2. my bad - if you add if not new_value: return None (or [] for Multi...) after the call to (Multi)ChoiceField?.clean(), it should do the trick... you don't have to worry explicitly about the required because its handled by the ChoiceField?.clean()
  3. always good news ;)

thanks for working on the test, I really appreciate it.

(follow-up: ↓ 8 ) 01/19/07 00:56:40 changed by floguy@gmail.com

Firstly, thanks for the quick response to my first batch of questions!

Secondly, I have uploaded an updated patch with new unit tests for the new Fields themselves, but I'm having a problem with getting everything to work correctly. When a form is gotten using form_for_instance, then calling the .save() function on that form throws errors. I tried tracing it through, and am having problems finding out what is going wrong. To recreate this, try running runtests.py model_forms.

Please let me know if I've got my environment set up properly, or if this is a legitimate error. Sorry if I'm moving slowly on this, but I'm relatively inexperienced with this process ;)

(in reply to: ↑ 7 ) 01/19/07 03:54:48 changed by Honza Kral <honza.kral@gmail.com>

Replying to floguy@gmail.com:

Firstly, thanks for the quick response to my first batch of questions! Secondly, I have uploaded an updated patch with new unit tests for the new Fields themselves, but I'm having a problem with getting everything to work correctly. When a form is gotten using form_for_instance, then calling the .save() function on that form throws errors. I tried tracing it through, and am having problems finding out what is going wrong. To recreate this, try running runtests.py model_forms. Please let me know if I've got my environment set up properly, or if this is a legitimate error. Sorry if I'm moving slowly on this, but I'm relatively inexperienced with this process ;)

I don't have access to a server right now, but try re-adding the section of the patch regarding django/newforms/models.py

01/19/07 15:29:11 changed by anonymous

You were right about that, I must have been crazy not to see it before. Now all tests pass including the new ones!

01/22/07 01:24:41 changed by anonymous

  • needs_tests deleted.

(follow-up: ↓ 14 ) 01/22/07 04:14:37 changed by Philipp Keller <philipp.keller@gmail.com>

Why is this in triage stage "design decision needed"? I don't find any open question. I'm looking forward for this patch to be accepted. This improves form_for_model usage of newforms a lot!

(follow-up: ↓ 13 ) 01/22/07 09:16:56 changed by adrian

  • summary changed from [patch] added choice fields that return models to Change form_for_model() to return correct field for ForeignKeys and ManyToManyFields.
  • stage changed from Design decision needed to Accepted.

There are a couple of problems with this patch:

* ModelChoiceField and ModelMultipleChoiceField should not live in newforms/fields.py. The only part of the newforms library that knows anything about models is the file newforms/models.py. ModelChoiceField and ModelMultipleChoiceField should live either in django/db/models/fields/related.py or django/newforms/models.py.

* Instead of taking model and choices arguments, ModelChoiceField and ModelMultipleChoiceField should take a queryset argument, which would be a QuerySet instance.

* The unit tests in this patch should be moved to tests/modeltests/model_forms/models.py. Any test that requires a working database connection should live in tests/modeltests -- not in tests/regressiontests, as this one does.

(in reply to: ↑ 12 ) 01/22/07 10:52:58 changed by Honza Král <Honza.Kral@gmail.com>

Replying to adrian:

There are a couple of problems with this patch: * ModelChoiceField and ModelMultipleChoiceField should not live in newforms/fields.py. The only part of the newforms library that knows anything about models is the file newforms/models.py. ModelChoiceField and ModelMultipleChoiceField should live either in django/db/models/fields/related.py or django/newforms/models.py.

done, I moved the fields to django/newforms/models.py.

* Instead of taking model and choices arguments, ModelChoiceField and ModelMultipleChoiceField should take a queryset argument, which would be a QuerySet instance.

done - problem here now is, if the field is not required, how do you add the ("", "------") option? I don't like the idea of it magically appearing in the choices, although it can be simply done.

I did't do anything about this, so the tests still fail for this feature...

* The unit tests in this patch should be moved to tests/modeltests/model_forms/models.py. Any test that requires a working database connection should live in tests/modeltests -- not in tests/regressiontests, as this one does.

done - I am sorry, I removed some of the simple tests (number of arguments etc),

(in reply to: ↑ 11 ) 01/23/07 00:23:46 changed by anonymous

Replying to Philipp Keller <philipp.keller@gmail.com>:

Why is this in triage stage "design decision needed"? I don't find any open question. I'm looking forward for this patch to be accepted. This improves form_for_model usage of newforms a lot!

Well, this has been accepted by Adrian in the meantime. My rationale was that this is adding a new class, ModelChoiceField?, so I counted it as something that needs a decision whether it's wanted. Actually, it's a corner case, and I don't use the newforms, so had to make a kind of summary decision.

01/23/07 00:24:12 changed by mir@noris.de

Pardon, the last one was me.

01/23/07 04:24:36 changed by Michael Radziej <mir@noris.de>

  • cc changed from Honza.Kral@gmail.com, floguy@gmail.com to Honza.Kral@gmail.com, floguy@gmail.com, gandalf@owca.info.

#3342 marked as duplicate

01/23/07 04:42:06 changed by Michael Radziej <mir@noris.de>

Hmm, the patch doesn't apply to current trunk, and the merge isn't trivial for me. Honza, can you take a look?

01/29/07 19:02:45 changed by anonymous

  • cc changed from Honza.Kral@gmail.com, floguy@gmail.com, gandalf@owca.info to Honza.Kral@gmail.com, floguy@gmail.com, gandalf@owca.info, kilian.cavalotti@lip6.fr.

01/30/07 13:06:51 changed by kilian.cavalotti@lip6.fr

  • attachment related_choice_fields_moved_trunk_resync.patch added.

Resynch'ed patch against trunk rev [4451]

01/31/07 10:09:07 changed by Honza Král <Honza.Kral@gmail.com>

  • attachment related_choice_field_empty_label.patch added.

added empty_label kwarg to the field, all tests pass now

01/31/07 10:11:32 changed by Honza Král <Honza.Kral@gmail.com>

  • needs_better_patch deleted.

the new patch contains everything necessary to pass the remaining tests, it applies against current trunk...

02/19/07 15:19:22 changed by anonymous

  • cc changed from Honza.Kral@gmail.com, floguy@gmail.com, gandalf@owca.info, kilian.cavalotti@lip6.fr to Honza.Kral@gmail.com, floguy@gmail.com, gandalf@owca.info, kilian.cavalotti@lip6.fr, yatiohi@ideopolis.gr.

02/19/07 18:33:51 changed by adrian

(Removed some of the old attachments to this ticket, to avoid confusion with the latest-and-greatest patches.)

02/19/07 20:42:07 changed by adrian

  • status changed from new to closed.
  • resolution set to fixed.

(In [4547]) Fixed #3257 -- Added newforms ModelChoiceField? and ModelMultipleChoiceField?, which are now used by form_for_model() and form_for_instance(). Thanks for the patch, Honza Kral, floguy@gmail.com and kilian.cavalotti


Add/Change #3257 (Change form_for_model() to return correct field for ForeignKeys and ManyToManyFields)




Change Properties
Action