Django

Code

Ticket #565 (new)

Opened 3 years ago

Last modified 2 weeks ago

Unique fields don't work on edit_inline models

Reported by: kyrrigle@gmail.com Assigned to: nobody
Component: Admin interface Version: SVN
Keywords: admin edit inline unique error Cc: kyrrigle@gmail.com, ep@acm.org, christoph.rauch@gmail.com, frankie@grimboy.co.uk, mail@janoberst.com, jarek.zgoda@gmail.com, avinash@sultanpur.org, Django565@JasonGreen.Name, matiassurdi@gmail.com
Triage Stage: Accepted Has patch: 0
Needs documentation: 0 Needs tests: 0
Patch needs improvement: 0

Description

There seems to be a bug with validating inline editable items that have unique=true.

To reproduce:

1) Modify the tutorial polls app so that the Choice model defines choice as:

    choice = meta.CharField(maxlength=200, core=True, unique=True)

2) Create a poll with two choices, click 'save and continue editing'

3) Add a third choice (with unique name) and click 'save'

You should see a "Choice with this choice already exists." validation error for the second choice.

Attachments

Change History

04/08/06 12:53:12 changed by david@zettazebra.com

  • keywords set to admin edit inline unique error.
  • version set to magic-removal.

This seems to apply to models that don't themselves contain a unique field but references a model that does.

For example, I've got a model NetworkAddressMap? that relates 3 separate models: NetworkIpAddress?, NetworkMacAddress?, and Hardware. NetworkAddressMap? has foreign keys for all of these models, and the one that points to Hardware specifies edit_inline=models.TABULAR. With this setup, I can add entries from the Hardware interface, but when I attempt to edit it after saving, I get this error:

Traceback (most recent call last): File "/usr/lib/python2.4/site-packages/Django-0.91-py2.4.egg/django/core/handlers/base.py" in get_response

  1. response = callback(request, *callback_args, **callback_kwargs)

File "/usr/lib/python2.4/site-packages/Django-0.91-py2.4.egg/django/contrib/admin/views/decorators.py" in _checklogin

  1. return view_func(request, *args, **kwargs)

File "/usr/lib/python2.4/site-packages/Django-0.91-py2.4.egg/django/views/decorators/cache.py" in _wrapped_view_func

  1. response = view_func(request, *args, **kwargs)

File "/usr/lib/python2.4/site-packages/Django-0.91-py2.4.egg/django/contrib/admin/views/main.py" in change_stage

  1. new_object = manipulator.save(new_data)

File "/usr/lib/python2.4/site-packages/Django-0.91-py2.4.egg/django/db/models/manipulators.py" in save

  1. if rel_new_data[related.opts.pk.name][0]:

KeyError? at /admin/trak/hardware/3/ 'id'

I can edit any of these models individually, but when edited "inline" I get the above error.

04/08/06 20:30:58 changed by david@zettazebra.com

Disregard my previous addendum to this ticket. The error I described has nothing to do with this.

05/29/06 15:52:00 changed by anonymous

Similar goes here:

SVN: rev. 3002 (MR)

from django.db import models

class Customer( models.Model ):

name = models.CharField?( maxlength=20, blank=False, unique=True ) def str( self ):

return self.name

class Admin:

pass

class Komputer( models.Model ):

Customer = models.ForeignKey?( Customer, edit_inline=models.TABULAR, num_in_admin=1) hwaddr = models.CharField?( maxlength=17, unique=True, core=True )

def str(self):

return self.hwaddr

class Admin:

pass

Adding new Customer(and Komputer)... when added individually no problem occurs

Traceback (most recent call last): File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/Django-0.91-py2.4.egg/django/core/handlers/base.py" in get_response

  1. response = callback(request, *callback_args, **callback_kwargs)

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/Django-0.91-py2.4.egg/django/contrib/admin/views/decorators.py" in _checklogin

  1. return view_func(request, *args, **kwargs)

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/Django-0.91-py2.4.egg/django/views/decorators/cache.py" in _wrapped_view_func

  1. response = view_func(request, *args, **kwargs)

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/Django-0.91-py2.4.egg/django/contrib/admin/views/main.py" in add_stage

  1. errors = manipulator.get_validation_errors(new_data)

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/Django-0.91-py2.4.egg/django/forms/init.py" in get_validation_errors

  1. errors.update(field.get_validation_errors(new_data))

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/Django-0.91-py2.4.egg/django/forms/init.py" in get_validation_errors

  1. self.run_validator(new_data, validator)

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/Django-0.91-py2.4.egg/django/forms/init.py" in run_validator

  1. validator(new_data.get(self.field_name, ), new_data)

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/Django-0.91-py2.4.egg/django/db/models/fields/init.py" in manipulator_validator_unique

  1. old_obj = self.manager.get(**{lookup_type: field_data})

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/Django-0.91-py2.4.egg/django/db/models/manager.py" in get

  1. return self.get_query_set().get(*args, **kwargs)

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/Django-0.91-py2.4.egg/django/db/models/query.py" in get

  1. obj_list = list(clone)

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/Django-0.91-py2.4.egg/django/db/models/query.py" in iter

  1. return iter(self._get_data())

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/Django-0.91-py2.4.egg/django/db/models/query.py" in _get_data

  1. self._result_cache = list(self.iterator())

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/Django-0.91-py2.4.egg/django/db/models/query.py" in iterator

  1. select, sql, params = self._get_sql_clause()

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/Django-0.91-py2.4.egg/django/db/models/query.py" in _get_sql_clause

  1. tables2, joins2, where2, params2 = self._filters.get_sql(opts)

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/Django-0.91-py2.4.egg/django/db/models/query.py" in get_sql

  1. tables2, joins2, where2, params2 = val.get_sql(opts)

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/Django-0.91-py2.4.egg/django/db/models/query.py" in get_sql

  1. return parse_lookup(self.kwargs.items(), opts)

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/Django-0.91-py2.4.egg/django/db/models/query.py" in parse_lookup

  1. tables2, joins2, where2, params2 = lookup_inner(path, clause, value, opts, opts.db_table, None)

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/Django-0.91-py2.4.egg/django/db/models/query.py" in lookup_inner

  1. raise TypeError?, "Cannot resolve keyword '%s' into field" % name

TypeError? at /admin/siec/customer/add/ Cannot resolve keyword 'hwaddr' into field

sqlite ver. 3.3.4

06/21/06 03:26:29 changed by Seer

  • summary changed from Unique fields don't work on edit_inline models to Unique fields don''t work on edit_inline models.

Hi all im fine, gl all!

02/17/07 10:47:07 changed by Simon G. <dev@simon.net.nz>

  • version changed from magic-removal to SVN.
  • stage changed from Unreviewed to Accepted.

I've replicated this with the current SVN and this model:

from django.db import models

# Create your models here.
class Poll(models.Model):
    question = models.CharField(maxlength=200)
    pub_date = models.DateTimeField('date published')

    class Admin:
        pass

class Choice(models.Model):
    poll = models.ForeignKey(Poll, edit_inline=models.STACKED, num_in_admin=3)
    choice = models.CharField(maxlength=200, core=True, unique=True)
    votes = models.IntegerField(core=True)

    class Admin:
        pass

However, it raises an IntegrityError (column choice is not unique) and not just a warning.

02/17/07 10:47:22 changed by Simon G. <dev@simon.net.nz>

  • summary changed from Unique fields don''t work on edit_inline models to Unique fields don't work on edit_inline models.

05/23/07 05:29:49 changed by Emanuele Pucciarelli <ep@acm.org>

  • cc changed from kyrrigle@gmail.com to kyrrigle@gmail.com, ep@acm.org.

06/06/07 15:06:25 changed by kent37@tds.net

The root of this problem is in django/db/models/fields/__init__.py. When manipulator_validator_unique() is curried with a manipulator at line 250 (in 0.96 release), the manipulator.manager is the manager for the related model (the model being edited), not the model containing the ForeignKey. So the call to self.manager.get() in manipulator_validator_unique() (line 37) looks up the unique field in the wrong table.

07/04/07 07:55:12 changed by anonymous

  • cc changed from kyrrigle@gmail.com, ep@acm.org to kyrrigle@gmail.com, ep@acm.org, christoph.rauch@gmail.com.

08/25/07 18:03:42 changed by anonymous

  • cc changed from kyrrigle@gmail.com, ep@acm.org, christoph.rauch@gmail.com to kyrrigle@gmail.com, ep@acm.org, christoph.rauch@gmail.com, frankie@grimboy.co.uk.

08/30/07 22:44:20 changed by Simon G. <dev@simon.net.nz>

I've marked #5302, #526, #2470, #2641, #4639, #4490 as duplicates of this.

09/21/07 14:29:14 changed by Jan Oberst <mail@janoberst.com>

  • cc changed from kyrrigle@gmail.com, ep@acm.org, christoph.rauch@gmail.com, frankie@grimboy.co.uk to kyrrigle@gmail.com, ep@acm.org, christoph.rauch@gmail.com, frankie@grimboy.co.uk, mail@janoberst.com.

12/11/07 08:25:17 changed by zgoda

  • cc changed from kyrrigle@gmail.com, ep@acm.org, christoph.rauch@gmail.com, frankie@grimboy.co.uk, mail@janoberst.com to kyrrigle@gmail.com, ep@acm.org, christoph.rauch@gmail.com, frankie@grimboy.co.uk, mail@janoberst.com, jarek.zgoda@gmail.com.

01/09/08 10:25:10 changed by anonymous

  • cc changed from kyrrigle@gmail.com, ep@acm.org, christoph.rauch@gmail.com, frankie@grimboy.co.uk, mail@janoberst.com, jarek.zgoda@gmail.com to kyrrigle@gmail.com, ep@acm.org, christoph.rauch@gmail.com, frankie@grimboy.co.uk, mail@janoberst.com, jarek.zgoda@gmail.com, avinash@sultanpur.org.

01/15/08 10:18:47 changed by Kent Johnson <kent37@tds.net>

There is a similar problem using unique_together in a model that is edited inline in another model's admin interface. This causes an AttributeError, e.g. unique_together = (('person', 'blog', 'role'),) causes this error:

  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/db/models/fields/__init__.py" in get_manipulator_fields
  240. params['validator_list'].append(getattr(manipulator, 'isUnique%s' % '_'.join(field_name_list)))

  AttributeError at /admin/stats/blog/1262/
  'ChangeManipulator' object has no attribute 'isUniqueperson_blog_role'

02/04/08 12:20:48 changed by Jason Green <Django@JasonGreen.Name>

  • cc changed from kyrrigle@gmail.com, ep@acm.org, christoph.rauch@gmail.com, frankie@grimboy.co.uk, mail@janoberst.com, jarek.zgoda@gmail.com, avinash@sultanpur.org to kyrrigle@gmail.com, ep@acm.org, christoph.rauch@gmail.com, frankie@grimboy.co.uk, mail@janoberst.com, jarek.zgoda@gmail.com, avinash@sultanpur.org, Django565@JasonGreen.Name.

04/30/08 06:49:33 changed by Matias Surdi <matiassurdi@gmail.com>

  • cc changed from kyrrigle@gmail.com, ep@acm.org, christoph.rauch@gmail.com, frankie@grimboy.co.uk, mail@janoberst.com, jarek.zgoda@gmail.com, avinash@sultanpur.org, Django565@JasonGreen.Name to kyrrigle@gmail.com, ep@acm.org, christoph.rauch@gmail.com, frankie@grimboy.co.uk, mail@janoberst.com, jarek.zgoda@gmail.com, avinash@sultanpur.org, Django565@JasonGreen.Name, matiassurdi@gmail.com.

Add/Change #565 (Unique fields don't work on edit_inline models)




Change Properties
Action