Django filtering from other model -


for example have 2 models:

class symbol(models.model):     rowid = models.charfield(max_length=64, primary_key=true)  class symbolproperties(models.model):     symbol = models.foreignkey(symbol, to_field='rowid', db_column='symbol')     some_value = models.integerfield(default=0) 

and want filter symbol objects some_value field, model symbol have no relation symbolproperties.
can without creating foreign key in symbol model?

yes. when declare foreignkey on 1 model, reverse relationship added other 1 (see documentation).

you can access related field attribute (symbol.symbolproperties_set, or whatever name define using related_name keyword argument in model field definition) or reference in lookup:

symbol.objects.filter(symbolproperties__some_value=5) 

(add distinct() ensure result contains unique instances of symbol.)


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -