Django

Code

Ticket #7908 (closed: fixed)

Opened 4 months ago

Last modified 4 months ago

A ForeignKey of an abstract class can fail to validate

Reported by: rockmhoward@gmail.com Assigned to: nobody
Milestone: 1.0 Component: Core framework
Version: SVN Keywords: ForeignKey abstract
Cc: mk@spinlock.ch Triage Stage: Ready for checkin
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

A model with a ForeignKey? relating to an abstract class fails to validate by crashing in related.py line 605.

The example models.py is attached or see: http://dpaste.com/66858/

The traceback from manage.py validate: http://dpaste.com/66860/

Attachments

models.py (0.8 kB) - added by rock@rockhoward.com on 07/22/08 22:26:25.
a models.py file that fails during validation
note1.py (402 bytes) - added by rock@rockhoward.com on 07/23/08 20:30:11.
7908.patch (0.6 kB) - added by mk on 07/24/08 09:03:36.

Change History

07/22/08 22:26:25 changed by rock@rockhoward.com

  • attachment models.py added.

a models.py file that fails during validation

07/23/08 20:27:11 changed by rock@rockhoward.com

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

I tried adding a guard by replacing the offending line 605 as follows:

to_field = to_field if to_field == None:

if to._meta.pk is None:

raise ValueError?("The %s model does not have a primary key and therefore a ForeignKey? cannot relate to it." % to_name)

to_field = to._meta.pk.name

This is not a real fix as the validate command still crashes, but the error message is a bit nicer. A real fix has to go in the validate code.

07/23/08 20:30:11 changed by rock@rockhoward.com

  • attachment note1.py added.

07/24/08 09:03:36 changed by mk

  • attachment 7908.patch added.

07/24/08 09:06:31 changed by mk

  • cc set to mk@spinlock.ch.
  • has_patch set to 1.
  • stage changed from Unreviewed to Ready for checkin.

The attached patch gives a better(?) error message for both ForeignKey? and ManyToManyField?, using the syntax above and also using f.e. mine = models.ManyToManyField?('Attachment') (lazy loading)

08/15/08 11:13:44 changed by mtredinnick

  • milestone set to 1.0.

08/19/08 09:17:25 changed by russellm

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

(In [8442]) Fixed #7908: Added validation checks on attempts to create ForeignKey? and M2M relations with abstract classes. Thanks to Rock Howard for the report.


Add/Change #7908 (A ForeignKey of an abstract class can fail to validate)




Change Properties
Action