from django.shortcuts import render

# Create your views here.

from django.forms import ModelForm
from common.models import Audit

class AuditForm(ModelForm):
    class Meta:
        model = Audit
        exclude = ( 'cdate',
                    'mdate',
                    'cuser',
                    'muser',
        )

    def save(self, *args, **kwargs):
        kwargs['commit']=False
        obj = super(AuditForm, self).save(*args, **kwargs)

        if not 'cuser' in self.cleaned_data:
            self.cleaned_data['cuser'] = request.user

        self.cleaned_data['muser'] = request.user

        obj.save()


def save_audit_model(self, modelform, instance):
    obj = super(AuditForm, self).save(instance, commit=False)
    
    if not obj.cuser:
        obj.cuser = request.user
        
    obj.muser = request.user
        
    obj.save()




Create abstract AuditForm class to be used by Forms by classes that inherit from Audit model.
Create abstract AuditForm class to be used by Forms by classes that inherit from Audit model.


- from django.shortcuts import render
+ from django.forms import ModelForm
+ from common.models import Audit
  
- # Create your views here.
+ class AuditForm(ModelForm):
+     class Meta:
+         model = Audit
+         exclude = ( 'cdate',
+                     'mdate',
+                     'cuser',
+                     'muser',
+         )
  
+     def save(self, *args, **kwargs):
+         kwargs['commit']=False
+         obj = super(AuditForm, self).save(*args, **kwargs)
+ 
+         if not 'cuser' in self.cleaned_data:
+             self.cleaned_data['cuser'] = request.user
+ 
+         self.cleaned_data['muser'] = request.user
+ 
+         obj.save()
+ 
+ 
+ def save_audit_model(self, modelform, instance):
+     obj = super(AuditForm, self).save(instance, commit=False)
+     
+     if not obj.cuser:
+         obj.cuser = request.user
+         
+     obj.muser = request.user
+         
+     obj.save()
+ 
+ 
+ 
+ 
Create abstract AuditForm class to be used by Forms by classes that inherit from Audit model.
## Code Before:
from django.shortcuts import render

# Create your views here.

## Instruction:
Create abstract AuditForm class to be used by Forms by classes that inherit from Audit model.
## Code After:
from django.forms import ModelForm
from common.models import Audit

class AuditForm(ModelForm):
    class Meta:
        model = Audit
        exclude = ( 'cdate',
                    'mdate',
                    'cuser',
                    'muser',
        )

    def save(self, *args, **kwargs):
        kwargs['commit']=False
        obj = super(AuditForm, self).save(*args, **kwargs)

        if not 'cuser' in self.cleaned_data:
            self.cleaned_data['cuser'] = request.user

        self.cleaned_data['muser'] = request.user

        obj.save()


def save_audit_model(self, modelform, instance):
    obj = super(AuditForm, self).save(instance, commit=False)
    
    if not obj.cuser:
        obj.cuser = request.user
        
    obj.muser = request.user
        
    obj.save()



