On Jan 4, 8:30 pm, Patrick <pstei...@gmail.com> wrote: > Ok, I will rephrase that with a concrete example: > I have those to models: > > class Modelo(models.Model): > nome = models.CharField(max_length=10) > manageable = models.BooleanField() > > class Equipamento(models.Model): > modelo = models.ForeignKey(Modelo) > nome = models.CharField(max_length=20) > #manageable = models.BooleanField() > > You see, i want to be able to filter the Equipamento objects based on > the value previously attributed in Modelo, but it seems to be not > possible, since 'manageable' is not an field in Equipamento. But the > workaround would be, create that manageable field, but make it auto- > populated based on the value already defined in the Modelo object, and > hiding it from the user, at the admin page. > > How could I accomplished that? > > Any help, tips, or different approach to accomplish are welcome. > > Thank you in advance.
I don't know why you say filtering Equipamento based on the value in Model 'seems to be not possible'. On the contrary, it is perfectly possible - that's the whole point of a relational database system. You want something like: Equipamento.objects.filter(modelo__manageable=True) This is documented here: http://docs.djangoproject.com/en/dev/topics/db/queries/#lookups-that-span-relationships The alternate method that you describe - adding manageable to Equipamento and populating it automatically - is called denormalization, and it's worthwhile only if you are doing a large amount of these lookups and database performance is a concern. Currently there's not built-in way to do it automatically - although there are interesting discussions going on on the developers list. You could still do it manually by defining a post-save signal for Modelo that updates all related Equipamento instances - see here for more information on signals: http://docs.djangoproject.com/en/dev/topics/signals/ -- DR. --~--~---------~--~----~------------~-------~--~----~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---