Sunday, March 23, 2014

Re: loaddata not looking for natural keys

Ok, now I am really confused.

Here's the code where the error originates in django/core/serializers/python.py:

           # Handle FK fields
            elif field.rel and isinstance(field.rel, models.ManyToOneRel):
                if field_value is not None:
                    import pdb; pdb.set_trace()
                    if hasattr(field.rel.to._default_manager, 'get_by_natural_key'):
                        if hasattr(field_value, '__iter__'):
                            obj = field.rel.to._default_manager.db_manager(db).get_by_natural_key(*field_value)
                            value = getattr(obj, field.rel.field_name)
                            # If this is a natural foreign key to an object that
                            # has a FK/O2O as the foreign key, use the FK value
                            if field.rel.to._meta.pk.rel:
                                value = value.pk
                        else:
                            value = field.rel.to._meta.get_field(field.rel.field_name).to_python(field_value)
                        data[field.attname] = value
                    else:
                        data[field.attname] = field.rel.to._meta.get_field(field.rel.field_name).to_python(field_value)
                else:
                    data[field.attname] = None



Here's what pdb says:
(Pdb) p field
<django.db.models.fields.related.ForeignKey: event_type>
(Pdb) p field_value
u'Cash Contribution'
(Pdb) p hasattr(field.rel.to._default_manager, 'get_by_natural_key')
True
(Pdb) n
> /home/bob/.virtualenvs/vn2/lib/python2.6/site-packages/django/core/serializers/python.py(109)Deserializer()
-> if hasattr(field_value, '__iter__'):
(Pdb) p hasattr(field_value, '__iter__')
False
(Pdb) p field_value
u'Cash Contribution'
(Pdb) p type(field_value)
<type 'unicode'>
(Pdb) n
> /home/bob/.virtualenvs/vn2/lib/python2.6/site-packages/django/core/serializers/python.py(117)Deserializer()
-> value = field.rel.to._meta.get_field(field.rel.field_name).to_python(field_value)
(Pdb) n
ValidationError: Validati...teger."])
> /home/bob/.virtualenvs/vn2/lib/python2.6/site-packages/django/core/serializers/python.py(117)Deserializer()
-> value = field.rel.to._meta.get_field(field.rel.field_name).to_python(field_value)

I thought unicode fields had iterators? But here's one in ipython:

In [15]: field_value = u'Cash Contribution'

In [16]: field_value.__iter__()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)

/home/bob/.virtualenvs/vn2/valuenetwork/<ipython console> in <module>()

AttributeError: 'unicode' object has no attribute '__iter__'






--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3b3e1377-bb7f-4b6c-812d-52a5d274ee40%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment