How to overide django modelform to achieve custom behaviour -
i have item object has manytomany relation object option. create modelform item object so;
class item(models.model): category = models.foreignkey(category) name = models.charfield(max_length=200) price = models.decimalfield(max_digits=9, decimal_places=2, blank=true, null=true) options = models.manytomanyfield(option) class optionform(modelform): options = forms.choicefield(widget=forms.radioselect()) class meta: model = item fields = ( 'options', )
when render form in template renders available options
item
object(the expected behavior) not created specific item. want able load options
defined specific item chosen user. how override form achieve such behavior.for example without form can render items
own options
through id. item = item.objects.get(pk=id)
its tough make modelform's defined on fly because intimately tied structure in model. nevertheless use clever template control flow , view rendering desired effect. untested millage might vary.
<form method="post" action=""> {{ formset.management_form }} {% form in formset %} {{ form.id }} <ul> {% if user_option.category %} <li>{{ form.caregory }}</li> {% endif %} {% if user_option.name %} <li>{{ form.name }}</li> {% endif %} {% if user_option.p_opt %} <li>{{ form.price }}</li> <li>{{ form.options }}</li> {% endif %} </ul> {% endfor %} </form>
from djano docs here.
Comments
Post a Comment