On 1 août, 17:16, eduardoorige <[email protected]> wrote:
> Good morning people.
> I made a signal that plays m2m table values to a dictionary.
> For example:
>
> dic = {'SECTOR': {'forbidden': [Genres ,...], 'released':
> [Genres ,...]}}
>
> There is a table of company sectors, each of these sectors has
> access rules prohibited and released. The genres are content that
> staff can not access, for example pornography, sports,
> etc ...
>
> When this rule except for the FIRST TIME, I get the SECTORS but
> Gender is empty.
> dic = {'FINANCIAL: {' forbidden ': [],' released ': []}}
What do you mean by "the first time" ?
> Here's the function that fulfills this dictionary:
>
> def dic_acesso():
> regra = Acesso.objects.filter(status=True)
> dic_acesso = {}
> for ind in regra:
> ind_s = str(ind)
I assume your Acesso model has some field like "name" or "label" or
whatnot. Using this attribute might help wrt/ readabily, robustness
*and* performances.
> dic_acesso[ind_s] = {}
> lista_genero_proibido = []
> lista_genero_liberado = []
> for gp in ind.proibido.all():
> lista_genero_proibido.append(str(gp))
> for gl in ind.liberado.all():
> lista_genero_liberado.append(str(gl))
Same remark as above, and this time you could save a *lot* of overhead
using Queryset.values_list - you could get the full list in one single
call and avoid uselessly building full model instances.
Assuming the field used for Acesso.__unicode is named "sector" and the
field used by Genero.__unicode__ is named 'label', the whole function
would look like this:
def get_dic_acesso():
regra = Acesso.objects.filter(status=True)
dic_acesso = {}
for ind in regra:
dic_acesso[ind.name] = {
"proibido": ind.proibido.values_list('label', flat=True),
"liberado": ind.liberado.values_list('label', flat=True)
}
return dic_acesso
Now this obviously won't solve your problem, but since your function
should work as it is (at least I didn't spot any logic error), I think
the problem is elsewhere, and probably depends on what you mean by
"the first time" (or when and where this "first time" append, etc).
Just a hint: if you're using MySQL, the default isolation level can
lead to rather suprinsing behaviours...
My 2 cents...
--
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.