Thursday, September 26, 2013

Re: IntegrityError with proxy model but not with parent?


As long as I know, a model form needs the fields to be included in order to get validation working, so I suspect you did something like this:

class ProxyForm(forms.ModelForm):
    class Meta:
        model = models.Myproxy
        fields = ('name', )

Since stuff field is not included in the form fields validation doesn't work for it, however you can get it working with something like this:

class ProxyForm(forms.ModelForm):
    stuff = forms.CharField(widget=forms.HiddenInput, initial='stuff')
    class Meta:
        model = models.Myproxy
        fields = ('name', 'stuff',)

Thought you probably need customize validation/error messages or you will get messages like:

Parent with this Name and Stuff already exists.

for a form that does´t include stuff as a hidden input.

El 25/09/2013, a las 20:15, Derek <> escribió:

I am working with a proxy model that does not seem to work with the 'unique_together' of the model being proxied.

What I have is:

class Parent(Model):
    id = AutoField(primary_key=True)
    name = CharField()
    stuff = CharField()

    class Meta:
        ordering = ['name']
        unique_together = (('name', 'stuff'),)

class Myproxy(Parent):

    class Meta:
        proxy = True

    def __init__(self, *args, **kwargs):
        super(Myproxy, self).__init__(*args, **kwargs)
        self.stuff = "test"

If I use the Parent model and try to enter, 'myname' and 'test' as the name/stuff entries twice, I get the usual warning on the second attempt; the admin telling me that this model entry already exists.
However, if I try the same thing on the Myproxy model (which has a form without the stuff field; as its meant to be a preset value), I get an:

IntegrityError at /admin/....../add/
(1062, "Duplicate entry 'myname-test' for key 'name'")

The stack trace is as follows:

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/" in wrapper
  372.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/" in _wrapped_view_func
  89.         response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/" in inner
  202.             return view(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/" in _wrapper
  25.             return bound_func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/" in bound_func
  21.                 return func(self, *args2, **kwargs2)
File "/usr/local/lib/python2.7/dist-packages/django/db/" in inner
  223.                 return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/" in add_view
  1007.                 self.save_model(request, new_object, form, False)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/" in save_model
File "/usr/local/lib/python2.7/dist-packages/django/db/models/" in save
  546.                        force_update=force_update, update_fields=update_fields)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/" in save_base
  591.                                update_fields=update_fields)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/" in save_base
  650.                 result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/" in _insert
  215.         return insert_query(self.model, objs, fields, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/" in insert_query
  1675.     return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/" in execute_sql
  937.             cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/" in execute
  41.             return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/" in execute
  122.             six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/" in execute
  120.             return self.cursor.execute(query, args)
File "/usr/lib/python2.7/dist-packages/MySQLdb/" in execute
  174.             self.errorhandler(self, exc, value)
File "/usr/lib/python2.7/dist-packages/MySQLdb/" in defaulterrorhandler
  36.     raise errorclass, errorvalue

(FTR, I have also tried overriding the model save(), but again get an IntegrityError, instead of Django performing the expected check).

I am obviously missing some key piece of code, but cannot see what it is.


You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
For more options, visit

No comments:

Post a Comment