python - How to make Django ManyToMany relationships explicit on the receiving model's end -
relationships, particularly manytomany
, in django have bothered me somewhat. in particular, since relationship defined in 1 of models, can't tell looking @ paired model other relationships might hiding.
for example, django documentation:
class topping(models.model): # ... class pizza(models.model): # ... toppings = models.manytomanyfield(topping)
you can tell looking @ code i'd find out relevant toppings pizza @ pizza.toppings
. cannot tell able tell pizzas have topping @ topping.pizza_set
--you have @ pizza
class see this.
as result, looking @ toppings
, don't know full range of fields has.
is there way around or make more explicit? or there i'm missing?
this seems unavoidable side effect of dry principle. don't know of way declaratively show symmetry in these relations (other commenting , such). if want make things explicit put relationship in own table (which django doing behind scenes anyway), like:
class topping(models.model): # ... class pizza(models.model): # ... class pizzatoppings(models.model): # '+' disables reverse relationship pizza = models.foreignkey(pizza, related_name='+') topping = models.foreignkey(topping, related_name='+')
... of course you'd lose of convenience of orm.
Comments
Post a Comment