Monday, August 30, 2010

Re: Model validation for non-django-orm databases

On 30/08/10 15:12, Owen Nelson wrote:

> I just started to wonder if the ORM would, I don't
> know... "freak out" on the off-chance I run a query that returns a set of
> objects with duplicate values on what is supposed to be the pk.

Nah, you just get multiple objects back with the same "pk", mapped from
different rows. At least, at the moment...

But therefore one other thing to watch out for is that such multiple
objects themselves will, unless you take further steps, compare True
under '==', even if they really represent different rows in the
underlying db, since django basically checks if self.pk == other.pk [1]

# this is a model wrapping a table with a key (bug_id, field_id),
# and field_id is the arbitrarily chosen fake pk for ORM purposes
l = PMSM_CustomFieldString.objects.filter(field_id=4)
a = l[0]
b = l[1]
# those are definitely different objects for different rows, but:
print a.bug_id == b.bug_id
False
print a is b
False
print a == b
True

And also watch out for things like hashing, as a few lines below [1]
shows, django also similarly uses the pk as the __hash__ of such objects.

If you're anything like us, you're probably just iterating through
queryset results to suck data out, so it doesn't matter too much. You
could just make sure to use field values from the objects, not the
object itself, or maybe overriding the relevant __blah__ would work.

Mind you, if you do have very complicated needs, do also bear in mind a
whole other library, SQLAlchemy, does have composite field support, it's
just generally more complex than Django ORM.

[1]
http://code.djangoproject.com/browser/django/trunk/django/db/models/base.py#L355

--
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