How can I have field dependency?
Case 1: If boolean field call_me is set, then telephone must be set, else it should be blank
Case 2: If many to many field category (with values sale, rent) has one of the values as sale, then price_sale must be set, else it should be blank
For Case 1, you can validate that easily in the model’s
cleanmethod:For Case 2, M2M relationships are not added until after the model is saved, so using
cleanon your model won’t work in this scenario. However, you can do this from thecleanmethod of anyModelFormyou use to edit this, be it in the admin or your own view.However, having
categoryas a M2M when the only possible values are “sale” and “rent”, is poor design. Even then, “sale” and “rent” are mutually exclusive, so an M2M is inappropriate anyways (your model won’t be experiencing both a “sale” and a “rent” at the same time ever).As a result, it would be a better idea to have
categorybe aCharFieldwithchoicesconsisting of “sale” and “rent”. If you do it that way, you can then use your model’scleanmethod in the same way as Case 1 for this as well.