Django

Code

Ticket #4928 (closed: invalid)

Opened 1 year ago

Last modified 1 year ago

unique_together bug when it's not a tuple. Validation missing

Reported by: MarioGonzalez <gonzalemario @t gmail.com> Assigned to: deryck
Milestone: Component: Core framework
Version: SVN Keywords:
Cc: gonzalemario@gmail.com Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 1

Description

if unique_together is a tuple with strings, Django will manage the content as characters. So I wrote a patch to validate that the unique_together content should be a tuple of tuples.

Attachments

unique_together (1.7 kB) - added by MarioGonzalez <gonzalemario @t gmail.com> on 07/19/07 17:21:32.
unique_together.diff (0.6 kB) - added by MarioGonzalez <gonzalemario @t gmail.com> on 07/31/07 08:12:08.
It just needed a very little change :-)

Change History

07/19/07 17:21:32 changed by MarioGonzalez <gonzalemario @t gmail.com>

  • attachment unique_together added.

07/29/07 22:52:53 changed by SmileyChris

  • needs_better_patch set to 1.
  • stage changed from Unreviewed to Design decision needed.
  • needs_tests changed.
  • needs_docs changed.

The current patch uses type(x) == ... when it should be using isinstance(x, (list, tuple)). New error wording could be polished a bit too.

Pushing to design decision to see if the idea in general is deemed worthy.

07/29/07 23:59:13 changed by ubernostrum

Personally, if we're going to type-check the value I think we should take the opposite approach -- use if isinstance(val, basestring) to only catch things that are strings. Limiting to a tuple or a list, while it fits with common practice, does limit the types of iterable things people could use in unique_together, and I wouldn't want to do that needlessly.

07/30/07 08:23:21 changed by MarioGonzalez <gonzalemario @t gmail.com>

Hi ubernostrum, I think you've got a good point of view. However almost every setting in Django that needs more than one value, use tuples or lists; if all should be a string, how do you transform unique_together = (('field1', 'field2'),)? ex: unique_together="field1, field2" or unique_together="'field1, field2' 'field1, field3'"

I mean, I think it's hard (not impossible) not use lists or tuples in those situations and also IMHO that's "Design decision".

07/30/07 15:21:18 changed by mtredinnick

  • stage changed from Design decision needed to Accepted.

I agree with James, the error checking is backwards here (and the above comment misunderstand the point James is making I suspect). Do a check like

if isinstance(val, basestring):
    raise TypeError, '....'

and then no further indenting is needed for the common case. It wasn't immediately clear from the bug description, but I gather you are just trying to work around the case of when people use a string when a tuple is required. So test for that one case, instead of ruling out iterators that are permissible.

If it's a simple check like this, catching a common error case, we can include it.

07/31/07 08:12:08 changed by MarioGonzalez <gonzalemario @t gmail.com>

  • attachment unique_together.diff added.

It just needed a very little change :-)

07/31/07 08:28:12 changed by MarioGonzalez <gonzalemario @t gmail.com>

Also, please change the error message. I'm not a English native speaker but I think you have noticed this :-)

08/03/07 20:29:40 changed by MarioGonzalez <gonzalemario @t gmail.com>

Is this ticket going to be added to the trunk?

09/14/07 11:14:37 changed by deryck

  • owner changed from nobody to deryck.
  • status changed from new to assigned.

09/14/07 11:36:48 changed by deryck

  • status changed from assigned to closed.
  • resolution set to invalid.

Closing as invalid since existing validation will error on the case that the field name doesn't exist. In others words, the following already fails validation...

unique_together = 'foo, bar'

or

unique_together = ('foo', 'bar')

so why should we test for the single case of unique_together being a string?

However, we should probably allow the single tuple, rather than the tuple of tuples for convenience. I've opened #5460 for this new issue.


Add/Change #4928 (unique_together bug when it's not a tuple. Validation missing)




Change Properties
Action