I need to render some form fields by hand in order to meet style 
requirements. I am using UpdateView instead of a ModelForm since I just 
need a couple of foreign key fields that are already defined on the model, 
and I am using html <select> elements to prevent invalid data from being 
entered. 

I'm running into trouble because when I choose an option and click the 
submit button, Django rejects the input and raises a validation error 
because the field is required. I know the data should be valid because it 
all came straight from the database, but Django appears to be throwing it 
away. I can't find UpdateView's clean() methods to try to catch the error 
that way. I've tried overriding post() just to see where the problem is 
occurring, but the option I chose seemed to just disappear; I don't know 
what Django is doing with it or why it thinks its invalid, so I don't know 
where to try to catch and fix the problem.

I have two separate html forms with two submit buttons; I separated them 
because they seemed easier to style that way, and also because users will 
generally need to change one or the other, not both; but two forms might be 
the wrong way to go about it. But that doesn't explain why data that came 
straight from the database is invalid. They both fail with the same 
ValidationErrors (i.e. fields are required). 

Thank you for any ideas or insight!
Heather


class Order(models.Model):

    order_id = models.IntegerField(primary_key=True)


    # (Some additional model fields.)

    service_level = models.ForeignKey('manifest.ServiceLevel', 
on_delete=models.PROTECT)
    priority = models.ForeignKey('fulfillment.Priority', 
on_delete=models.PROTECT)



class Priority(models.Model):
    """fulfillment priority levels"""
    TYPES = Choices(
        ('FR', 'FLASH_RUSH', 'Flash Rush'),
        ('RU', 'RUSH', 'Rush'),
        ('RG', 'REGULAR', 'Regular'),
    )

    code = models.CharField(max_length=2, choices=TYPES)
    name = models.CharField(max_length=50)



class ServiceLevel(models.Model):
    """levels of service which a carrier provides"""

    code = models.CharField(max_length=10, primary_key=True)
    name = models.CharField(max_length=50)

    # (Some additional model fields.)



class WMSOrderUpdateView(UpdateView):
    context_object_name = 'order'
    template_name = 'fulfillment/order_detail.html'
    fields = ['priority', 'service_level']

    def get_context_data(self, **kwargs):
        context = super().get_context_data()
        context['priorities'] = Priority.objects.all()
        context['ship_methods'] = ServiceLevel.objects.all()
        return context



<ul class="list-group-flush">
    <li class="list-group-item">
        <form method="post">
            {% csrf_token %}
            <label for="priority">
                {% if order.priority.code == 'FR' %}
                    <span class="badge badge-pill badge-danger">
                {% elif order.priority.code == 'RU' %}
                    <span class="badge badge-pill badge-warning">
                {% else %}
                    <span class="badge badge-pill badge-success">
                Priority:&nbsp;
                {% endif %}
                </span>
            </label>
            <select id="priority" name="change_priority">
                {% for priority in priorities %}
                    <option value="{{ priority.id}}" {% if priority.code == 
order.priority.code %}selected{% endif %}>{{ priority.name }}</option>
                {% endfor %}
            </select>
            <input class="btn btn-primary" type="submit" value="Apply">
        </form>
    </li>

    <li class="list-group-item">
        <form method="post">
            {% csrf_token %}
            <label for="ship_method"><strong>Shipping Method: </strong></label>
            <select id="ship_method" name="change_method">
                {% for method in ship_methods %}
                    <option value="{{ method.code }}" {% if method.code == 
order.service_level.code %}selected{% endif %}>{{ method.name|title }}</option>
                {% endfor %}
            </select>
            <input class="btn btn-primary" type="submit" value="Apply">
        </form>
    </li>

    <!-- A few more <li> elements go here, but they're for display only. No 
form fields. -->
</ul>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/26b1dcbd-54c7-431c-bd0e-5740ba470aa3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to