So I solved my own problem. Apparently querysets pass the db row as
positional args, from which it constructs a new model instance. I had
overridden __init__ on my model to take an extra argument, which was
absorbing one of the positional args and 'causing the other values to
get incorrectly assigned.
Offending line (in my code was):
def __init__( self, amount=None, *args, **kwargs ):
super(...
Fixed by:
def __init__( self, *args, **kwargs ):
amount = kwargs.pop('amount',None)
super(...
Offending line in django/db/models/query.py
280 obj =
self.model(*row[index_start:aggregate_start])
Dunno if this'd be worth an explicit mention in documentation that
changing arguments by overriding __init__ on a model can cause
unexpected behavior. (Or maybe it's already in there, I just haven't
seen it yet.)
On Apr 5, 5:54 pm, Wedg <[email protected]> wrote:
> So I've come across a weird bug... just wanted to see if anyone's ever
> heard of anything like it...
> I've got a set of models which appear to create/save correctly. For
> example, if I print mymodel.fkfield_id on the instance before/after
> save, it's A-OK.
>
> However, when I try to load the models again, either in shell or in a
> view, the fields are all mixed up - one of the fields gets None as its
> value (it's a NOT NULL field in the SQL), the other four fields are
> consistently being switched - such that mymodel.fkfield_id is getting
> the value of one of the Decimal fields, and the self.id is getting the
> value of an fkfield... always the same order on the switching tho', as
> far as I can see.
>
> I checked the database - the rows are saved 100% a-ok in the database,
> so it appears that it's something in the loading.
>
> I feel like I should mention that this particular models inherits a
> abstract model class which has a GenericRelation (only that one
> field), which may/may not be affecting this - I have no idea.
>
> I should also note that in the process of bug-hunting I've done a ./
> manage reset of the app a few times - I just noticed the bug show up
> after the last reset, but I didn't change any model fields - I was
> only clearing out the data.
>
> So has anyone ever *heard* of anything like this? Or should I start
> posting code?
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.