On 13 déc, 12:20, Scoox <stephan.f.mul...@gmail.com> wrote:
> Hi Daniel,
> thanks for your answer. Sorry, cmp should be c. I tried to only give
> the import parts to make it more readable, but I made a mistake there.
> The model code is pretty standard, except for one function:
>
>     isEditable=0
>     login=0

Assuming these two previous lines are at the top-level of you class
statement, both attributes will be class-attributes (that is "shared
by all instances"). I don't think this is what you want.

>     def editable(self):
>         if self.login==0: return False
>         else:
>             if self.isEditable!=0: return self.isEditable
>             if self==self.login.get_profile():

Are you sure you want to compare the current whatever instance with a
profile ???

>                 self.isEditable=True
>                 for o in self.offer_set.all(): o.editable=True

Such a side-effect is really really bad practice (and, in this case,
mostly useless).

>                 return True
>             else:
>                 self.isEditable=False
>                 return False


Code depending on the request.user should either takes an user as
param or live elsewhere.

> (I added the isEditable attribute to cache if the object is editable,
> since I wasn't sure if get_profile() is lazy loaded or sends a request
> to the db with each iteration)

When you aren't sure, check, don't wildguess.

> That's probably right. But from what I understood, all() is a lazy
> loader, so when Django loaded the objects once out of the db, it
> should not do that again when the same object is called in a template.
> Or did I understand that wrong?

You did. What "lazy" means here is that no request is issued until you
start to iterate over the queryset or subscript it.

> Yes that sounds possible. Just wondering if there is a cleaner way to
> do it - the ideal way would of course every object having access to
> the user in some way, but that seems impossible.

Indeed. How would your code work in another environnment (command line
tool for example) else ?

wrt/ "a cleaner way to do it", your business rules are not clear
enough to come with a sensible answer.

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

Reply via email to