аватар question@mail.ru · 01.01.1970 03:00

Drop -down list in Django

I am writing a simple online store on django 1.9 . The following models are available:

tovar/models.py

    color  (models.model):  color = models.charfield (verbose_name = , max_length =  25 , BLANK =  false , null =  true )  code = models.charfield (Verbose_name = , max_length =  8 , Blank =  true , null =  true )    Tovar  (Models.model):  Color = Models.Manytomanyfield (Color, Blank =  true )  

card/models.py

  class   car  (models.model):  color = models.charfield (Verbose_name = , max_length =  25 , Blank =  false , null =  true )     

in the table in the table Cart CART apps is indicated a list of colors of a certain product that should be taken from the tovar (ManyTomanyfield fields) table. If you use the Selectmultiple widget, how to implement the output of Manytomanyfield?

аватар answer@mail.ru · 01.01.1970 03:00

You can in the template using a cycle in tags & lt; Select & gt; & lt; Option & GT; Place the desired information from the fields, and then connect for a beautiful multi -sector.

  & LT; Select  class  =  multiple = "" "" "" "" "" "" NAME =  redired =  "" "" "  id  =  "" Yourselectid ""  & gt;  { %  for  m  in  model_context %}  & lt; option value =  & gt; {m.someename}} & lt;/option & gt;  { % endfor %}  & lt;/Select & gt;    in your custom .js file, you can manipulate this thing, Tuing to the  id parameter  tag  & lt; select & gt; . DATA-HIGHLIGHTED = ""> $ (Document). DEADY (FUNCTION () { $ (). Multiselet ({ Columns:  1 ,  Placeholder: ,  search: false,  selectall: true}); });     

transmit information from this thing You can click on the button, it must be created in html.

  $ (Document) .Ready (function () { $ ( ""#SEND-BUTTON "" ). Click (Function () { var fdata = $ (). Serialize ();  $ .Ajax ({ URL: ,   type :  "post" ",  datatype:  "" json "" ,  data: fdata,  Success: function (json) { alrt (); },  error: function (xhr, errmsg, err) { alert ( "" cout not send url to django. error: ""  + xhr.status +  + xhr.responsetext); } }); }); });     

yes, with such a cod, you can not create a form at all. All magic takes place in views.py and custom.js .

Latest

Similar