Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango custom (multi)widget input validation
    primarykey
    data
    text
    <p>What is the correct method for validating input for a custom multiwidget in each of these cases:</p> <ol> <li>if I want to implement a custom Field?</li> <li>if I want to use an existing database field type (say DateField)?</li> </ol> <p>The motivation for this comes from the following two questions:</p> <ol> <li><a href="https://stackoverflow.com/questions/3511855/how-do-i-use-djangos-multiwidget/4663801#4663801">How do I use django's multi-widget</a>?</li> <li><a href="https://stackoverflow.com/questions/4662835/django-subclassing-multiwidget-reconstructing-date-on-post-using-custom-multiwi">Django subclassing multiwidget</a></li> </ol> <p>I am specifically interested in the fact that I feel I have cheated. I have used <code>value_from_datadict()</code> like so:</p> <pre><code>def value_from_datadict(self, data, files, name): datelist = [widget.value_from_datadict(data, files, name + '_%s' % i) for i, widget in enumerate(self.widgets)] try: D = date(day=int(datelist[0]), month=int(datelist[1]), year=int(datelist[2])) return str(D) except ValueError: return None </code></pre> <p>Which looks through the <code>POST</code> dictionary and constructs a value for my widget (see linked questions). However, at the same time I've tacked on some validation; namely if the creation of <code>D</code> as a date object fails, I'm returning <code>None</code> which will fail in the <code>is_valid()</code> check.</p> <p>My third question therefore is should I be doing this some other way? For this case, I do not want a custom field.</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload