0

I have 2 models

class WorkOrder(models.Model):
    work_order_id = models.AutoField(db_column='Work_order_id', 
        primary_key=True)
    work_order_number = models.CharField(db_column='Work_order_number', 
        max_length=50, blank=True, null=True)  
    work_order_date = models.DateField(db_column='Work_order_date', 
        blank=True, null=True)  
    customer = models.ForeignKey(Customer, db_column='Customer_id', 
        blank=True, null=True,on_delete=models.CASCADE)  
    description = models.CharField(db_column='Description', max_length=50, 
        blank=True, null=True)  
    completed_date = models.DateField(db_column='Completed_date', 
       blank=True, null=True)  
    completed = Bit1BooleanField(db_column='Completed', 
       editable=False,default=0)  

    class Meta:
        managed = False
        db_table = 'work_order'

class WorkOrderItem(models.Model):
    work_order_item_id = models.AutoField(db_column='Work_order_item_id', 
             primary_key=True)  # Field name made lowercase.
    work_order = models.ForeignKey(WorkOrder, models.DO_NOTHING, 
             db_column='Work_order_id')  # Field name made lowercase.
    part = models.ForeignKey(Part,db_column='Part_id', blank=True, 
         null=True,on_delete=models.CASCADE)  # Field name made lowercase.
    quantity = models.IntegerField(db_column='Quantity', blank=True, 
         null=True)  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 'work_order_item'

I am using a formset in forms.py

WorkOrderFormSet = inlineformset_factory(WorkOrder, WorkOrderItem,
                                     # fields='__all__'
                                     # widgets={'work_order_date' : 
DateInput(attrs={'type': 'datetime'}),},
                                     form=WorkOrderForm,
                                     extra=3, can_delete=False)

I want to add a widget for work_order_date in Workorder table.When I add it inside WorkOrderFormset, it shows an error that WorkOrderItem does not have that particular field. So I tried adding form=WorkOrderForm and adding Widget there as:-

class WorkOrderForm(forms.ModelForm):
    class Meta:
        model = WorkOrder
        fields = '__all__'  
        widgets={'work_order_date': DateInput(attrs={'type': 'datetime'}),}

But there is no change when it is displayed in the web page.

The view is:-

class CreateWorkOrderItem(CreateView):
    model=WorkOrder
    fields=['work_order_number','work_order_date','customer','description']
    success_url=reverse_lazy('WorkOrder:WorkOrderPending_list')

    def get_context_data(self, **kwargs):
        print("from get_context_data")
        data = super(CreateWorkOrderItem, self).get_context_data(**kwargs)
        if self.request.POST:
            data['workorder'] = WorkOrderFormSet(self.request.POST)
        else:
        data['workorder'] = WorkOrderFormSet()
        return data

    def form_valid(self, form):
        context = self.get_context_data()
        workorders = context['workorder']
        with transaction.atomic():
            self.object = form.save()

In the template I have:-

{% bootstrap_form form %}
  {{ workorders.management_form }}
  {% for form in workorder.forms %}
    {% bootstrap_form form %}
  {% endfor %}

Ps:-both the models are being displayed in webpage. There is only problem with the change in widget.In the documentation of django, they just mention that widget can be changed same as modelformset_factory. but in it,all models are same,so this error is not there. Using python 3.6.4 Django 2.0.2

If you need more information please ask. Thank you!

  • Adding`widgets` to `WorkOrderForm` should have worked. Check the html source of your page or inspect using firebug to see the type of the input. – art May 02 '18 at 07:01

1 Answers1

1

You need to set the form_Class in your View, instead of fields. The widgets specified to WorkOrderForm is enough.

class CreateWorkOrderItem(CreateView):
    model=WorkOrder
    #fields=['work_order_number','work_order_date','customer','description']
    form_class=WorkOrderForm

Edit:

Since inlineformset_factory() creates multiple forms for the sub-model, don't pass the modelFOrm of the parent form to it. The following would be enough.

WorkOrderFormSet = inlineformset_factory(WorkOrder, WorkOrderItem,
                                     extra=3, can_delete=False)

If you need to have custom form for the sub-model, then you may creatt and specify above.

art
  • 1,358
  • 1
  • 10
  • 24