def save(self):
if self.authorised.count() == 0:
self.is_private = False
else:
self.is_private = True
super(Topic, self).save()
But I found out that when creating a new Object and saving it that
is_private is always set to True. On further inspection I found out
that the "authorised" field is set to all users before saving but is
empty after saving.
>>> t = Topic()
>>> t.authorised.count()
2773L
>>> t.save()
>>> t.authorised.count()
0L
Is this the desired behavior?
> On further inspection I found out
> that the "authorised" field is set to all users before saving but is
> empty after saving.
>
> >>> t = Topic()
> >>> t.authorised.count()
>
> 2773L
>
> >>> t.save()
> >>> t.authorised.count()
>
> 0L
>
> Is this the desired behavior?
This is kind of a bug, caused by the fact that the ID of 't' is not set
until it is saved, and 'authorised' is a dynamic attribute that does a
database query on the many-to-many database table when you access it.
It's impossible to evaluate .authorised properly until you have saved
the object at least once.
It would be possible to get Django to throw an exception in this case.
What do people think about that?
Luke
--
"In my opinion, we don't devote nearly enough scientific research to
finding a cure for jerks." (Calvin and Hobbes)
Luke Plant || L.Plant.98 (at) cantab.net || http://lukeplant.me.uk/
>It would be possible to get Django to throw an exception in this case.
>What do people think about that?
>
>
I think it makes sense. As well as documenting that though dependent
objects (both M2M and 12M) look like they are belong to the parent they
really can't be processed in parents save() method because in DB they
are saved separately.