Monday, November 28, 2011

hasattr() and model attrname for foreign keys appending "_id"

Hi,

I'm new to django, so it's possible I'm doing something wrong.

I have a model defined that has the following field:

parent_host_uuid = models.ForeignKey(Mgmt_host_model,
to_field='uuid', blank=True, db_column="parent_host_uuid")

The database has created the column:

| parent_host_uuid |

I have a line in my code that checks first to see if the attribute
exists before I set it - doing hasattr(mobj, "parent_host_uuid")

This is failing but "parent_host_uuid_id" is there.

In the debugger:

(Pdb) print self.mobj.parent_host_uuid
*** DoesNotExist:
(Pdb) print self.mobj.parent_host_uuid_id
None

I'd rather not special case - looking specifically for individual
attributes that are foreign keys and have "_id" appended on the end so
I can set them.

What am I doing wrong? Or, is there way to get around this?

Thanks,

Dan

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment