On Thu, Jul 3, 2014 at 10:48 AM, Florian Apolloner
<f.apollo...@gmail.com> wrote:
>
>
> On Thursday, July 3, 2014 3:03:25 PM UTC+2, Jon Dufresne wrote:
>>
>> > Also, even if we find a place to show
>> > the errors, the user is (usually) in no position to correct them (after
>> > all,
>> > there is no field he could change to fix it).
>>
>> I don't follow. In my specific example the user is able to change the
>> "name" field. In my opinion, the form should fail to validate because
>> the _user_ entered "newname" twice, for two different names when they
>> should be unique. The user is in the position to 1) make these
>> conflict and 2) correct them.
>
>
> I don't follow; if the user is able to change the "name" field; it's
> included in the form and uniqueness is checked, I thought we are talking
> about fields __not__ included in a form.
>

The "name" field is included in the form, but the "container" field is
not. The name and container fields are unique_together. The formset
only validates unique fields when all fields are in the form. Here is
the example again.



models.py

----
class Container(models.Model):
    pass

class Item(models.Model):
    container = models.ForeignKey(Container)
    name = models.CharField(max_length=
100)

    class Meta:
        unique_together = ('container', 'name')

ItemFormSet = modelformset_factory(model=Item, fields=['name'])
----

tests.py
----
class ItemFormSetTestCase(TestCase):
    def test_unique_item_name(self):
        container = Container()
        container.save()
        item1 = Item(container=container, name='item1')
        item1.save()
        item2 = Item(container=container, name='item2')
        item2.save()
        data = {
            'form-TOTAL_FORMS': 2,
            'form-INITIAL_FORMS': 2,
            'form-MAX_NUM_FORMS': 2,
            'form-0-id': str(item1.pk),
            'form-0-name': 'newname',
            'form-1-id': str(item2.pk),
            'form-1-name': 'newname',
        }
        formset = ItemFormSet(
            data,
            queryset=Item.objects.filter(container=container))
        self.assertFalse(formset.is_valid())
---

This test fails because the uniqueness of name is not actually
validated. If I were to go ahead an save this "valid" form, I receive
the following error:

---
Traceback (most recent call last):
  File "/home/jon/djtest/djtest/myapp/tests.py", line 27, in
test_unique_item_name
    formset.save()
  File 
"/home/jon/djtest/venv/lib/python2.7/site-packages/django/forms/models.py",
line 636, in save
    return self.save_existing_objects(commit) + self.save_new_objects(commit)
  File 
"/home/jon/djtest/venv/lib/python2.7/site-packages/django/forms/models.py",
line 753, in save_existing_objects
    saved_instances.append(self.save_existing(form, obj, commit=commit))
  File 
"/home/jon/djtest/venv/lib/python2.7/site-packages/django/forms/models.py",
line 623, in save_existing
    return form.save(commit=commit)
  File 
"/home/jon/djtest/venv/lib/python2.7/site-packages/django/forms/models.py",
line 457, in save
    construct=False)
  File 
"/home/jon/djtest/venv/lib/python2.7/site-packages/django/forms/models.py",
line 103, in save_instance
    instance.save()
  File 
"/home/jon/djtest/venv/lib/python2.7/site-packages/django/db/models/base.py",
line 590, in save
    force_update=force_update, update_fields=update_fields)
  File 
"/home/jon/djtest/venv/lib/python2.7/site-packages/django/db/models/base.py",
line 618, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update,
using, update_fields)
  File 
"/home/jon/djtest/venv/lib/python2.7/site-packages/django/db/models/base.py",
line 680, in _save_table
    forced_update)
  File 
"/home/jon/djtest/venv/lib/python2.7/site-packages/django/db/models/base.py",
line 724, in _do_update
    return filtered._update(values) > 0
  File 
"/home/jon/djtest/venv/lib/python2.7/site-packages/django/db/models/query.py",
line 598, in _update
    return query.get_compiler(self.db).execute_sql(CURSOR)
  File 
"/home/jon/djtest/venv/lib/python2.7/site-packages/django/db/models/sql/compiler.py",
line 1003, in execute_sql
    cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
  File 
"/home/jon/djtest/venv/lib/python2.7/site-packages/django/db/models/sql/compiler.py",
line 785, in execute_sql
    cursor.execute(sql, params)
  File 
"/home/jon/djtest/venv/lib/python2.7/site-packages/django/db/backends/utils.py",
line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/jon/djtest/venv/lib/python2.7/site-packages/django/db/utils.py",
line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File 
"/home/jon/djtest/venv/lib/python2.7/site-packages/django/db/backends/utils.py",
line 65, in execute
    return self.cursor.execute(sql, params)
  File 
"/home/jon/djtest/venv/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py",
line 485, in execute
    return Database.Cursor.execute(self, query, params)
IntegrityError: UNIQUE constraint failed: myapp_item.container_id,
myapp_item.name
---

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/CADhq2b4LncsOvOuZ1vWNYm0XBQ7pAsz1u5k7VFhukt3DX-7jog%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to