Thursday, September 26, 2013

Re: IntegrityError with proxy model but not with parent?

Hi,

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.

HTH
El 25/09/2013, a las 20:15, Derek <gamesbook@gmail.com> 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:

Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in wrapper
  372.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/cache.py" in _wrapped_view_func
  89.         response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/sites.py" in inner
  202.             return view(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapper
  25.             return bound_func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in bound_func
  21.                 return func(self, *args2, **kwargs2)
File "/usr/local/lib/python2.7/dist-packages/django/db/transaction.py" in inner
  223.                 return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in add_view
  1007.                 self.save_model(request, new_object, form, False)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in save_model
  740.         obj.save()
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save
  546.                        force_update=force_update, update_fields=update_fields)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save_base
  591.                                update_fields=update_fields)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" 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/manager.py" in _insert
  215.         return insert_query(self.model, objs, fields, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" 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/compiler.py" in execute_sql
  937.             cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py" in execute
  41.             return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py" 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/base.py" in execute
  120.             return self.cursor.execute(query, args)
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py" in execute
  174.             self.errorhandler(self, exc, value)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py" 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.

Thanks
Derek


--
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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment