I have a working code which gets all incident details.
But i am unable to save complete data in database only the last record gets saved
def incidents(request):
incidentsServicenow = IncidentsServicenow()
c = pysnow.Client(instance='', user='', password='')
# Define a resource, here we'll use the incident table API
incident = c.resource(api_path='/table/incident')
print('incident', incident)
# Query for incidents with state 1
response = incident.get()
print('response', response)
# Iterate over the result and print out `sys_id` of the matching records.
ticket = []
for record in response.all():
data = {
'number': record['number'],
'description': record['description'],
'short_description': record['short_description'],
'state': record['state'],
}
#print(record['number'])
incidentsServicenow.number = data['number']
incidentsServicenow.title = data['short_description']
incidentsServicenow.description = data['description']
incidentsServicenow.state = data['state']
#ticket.append(data)
print("storing data")
incidentsServicenow.save()
return HttpResponse(ticket, content_type='application/json')
My model is
class IncidentsServicenow(models.Model):
number = models.CharField(max_length=32)
title = models.CharField(max_length=160)
description = models.TextField()
state = models.CharField(max_length=20)
class Meta:
managed = False
db_table = 'incidents_servicenow'
I need to save all the records in database
You should create objects in the loop. From the code, I can see that you created the incidentsServicenow object is created outside the loop.
for record in response.all():
data = {
'number': record['number'],
'description': record['description'],
'short_description': record['short_description'],
'state': record['state'],
}
#print(record['number'])
incidentsServicenow = IncidentsServicenow()
incidentsServicenow.number = data['number']
incidentsServicenow.title = data['short_description']
incidentsServicenow.description = data['description']
incidentsServicenow.state = data['state']
ticket.append(data)
print("storing data")
incidentsServicenow.save()
return HttpResponse(ticket, content_type='application/json')
or you could do like this
for record in response.all():
data = {
'number': record['number'],
'description': record['description'],
'short_description': record['short_description'],
'state': record['state'],
}
#print(record['number'])
incidentsServicenow = IncidentsServicenow(number=data['number'],
title=data['short_description'],description=data['description'],state=data['state'])
ticket.append(data)
print("storing data")
incidentsServicenow.save()
return HttpResponse(ticket, content_type='application/json')
Add below line inside for loop
incidentsServicenow = IncidentsServicenow()
Related
I am trying to pass a list of student IDs but it is not working.
from angular I am passing data as follows
let noticeData = this.announceForm.value;
if (noticeData.students.includes('all')){
noticeData.students = noticeData.students.filter((s) => s != 'all')
}
noticeData.students = JSON.stringify(noticeData.students);
rest call
var formData = new FormData();
for (var key in noticeData) {
formData.set(key, noticeData[key]);
}
this._rest.one("/classwork/notices/")
.post('', formData)
.toPromise()
.then((classworks) => {
}, (error) => {
console.log("ERROR SENDING ANNOUNCEMENT ", error);
this.alert = {
type : "error",
message: "Something went wrong, please try again."
}
});
}
from django i am reading and trying to store data as
Below is the to_internal_value method which I have overridden.
def to_internal_value(self, data):
tempdict = data.copy()
tempdict['students'] = json.loads(data['students'])
data = tempdict
return super(NoticesSerializer, self).to_internal_value(data)
views.py
class ClassworkView(APIView):
def get(self, request, format=None):
notices = ClassworksFilter(request.query_params, queryset=Notice.objects.all()).qs
materials = ClassworksFilter(request.query_params, queryset=Material.objects.all()).qs
assignments = ClassworksFilter(request.query_params, queryset=Assignment.objects.all()).qs
queryset = sorted(
chain(notices, materials, assignments),
key=lambda instance: instance.create_date
)
model_serializers = {
'notice': NoticesSerializer,
}
classworks = []
for instance in queryset:
serializer = model_serializers[instance._meta.model_name]
data = serializer(instance).data
data['classwork_type'] = instance._meta.model_name
classworks.append(data)
page_number = request.query_params.get('page', 1)
page_size = request.query_params.get('page_size', 20)
page = Paginator(classworks, per_page=page_size).get_page(page_number)
base_url = f'{request.scheme}://{request.get_host()}{reverse("classworks")}'
params = [f'{k}={v}' if k!='page' else None for k,v in request.query_params.items()]
indx = 0
for param in params:
if param==None:
params.pop(indx)
indx+=1
params = '&'.join(params)
next = f'{base_url}?page={page.next_page_number()}&{params}' if page.has_next() else None
prev = f'{base_url}?page={page.previous_page_number()}&{params}' if page.has_previous() else None
response = {
'count': len(classworks),
'next': next,
'previous': prev,
'results': page.object_list
}
return Response(response)
here is serializer.py
class NoticesSerializer(serializers.ModelSerializer):
class Meta:
model = Notice
fields = '__all__'
def to_representation(self, instance):
representation = super(NoticesSerializer, self).to_representation(instance)
try:
representation['classroom'] = ClassroomsSerializer(instance.classroom, context=self.context).data
students = StudentsSerializer(instance.students, many=True, context=self.context).data
for student in students:
student['classroom'] = student['classroom']['id']
representation['students'] = students
except Exception as error:
print("ERROR SETTING REPRESENTATION FOR NOTICE SERIALIZER", error)
return representation
here is the models.py
class Notice(Classwork):
file = models.FileField(upload_to='classworks/attachments', blank=True, null=True)
link = models.URLField(blank=True, null=True)
description = models.TextField()
class Meta(BaseModel.Meta):
db_table = 'ec_notice'
but it always returns the same error
{students: [“Incorrect type. Expected pk value, received list.”]}
here the data I am posting by form data
I'm trying to use bulk_create in order to add objects to related models. Here i'm fetching the csv file through post request which contains required fields. As of now I can add items to models which is unrelated using the csv file and bulk_create and it's working.
class BulkAPI(APIView):
def post(self, request):
paramFile = io.TextIOWrapper(request.FILES['requirementfile'].file)
dict1 = csv.DictReader(paramFile)
list_of_dict = list(dict1)
objs = [
ManpowerRequirement(
project=row['project'],
position=row['position'],
quantity=row['quantity'],
project_location=row['project_location'],
requested_date=row['requested_date'],
required_date=row['required_date'],
employment_type=row['employment_type'],
duration=row['duration'],
visa_type=row['visa_type'],
remarks=row['remarks'],
)
for row in list_of_dict
]
try:
msg = ManpowerRequirement.objects.bulk_create(objs)
returnmsg = {"status_code": 200}
print('imported successfully')
except Exception as e:
print('Error While Importing Data: ', e)
returnmsg = {"status_code": 500}
return JsonResponse(returnmsg)
My models are:
class ManpowerRequirement(models.Model):
project = models.CharField(max_length=60)
position = models.CharField(max_length=60)
quantity = models.IntegerField()
project_location = models.CharField(max_length=60)
requested_date = models.DateField()
required_date = models.DateField()
employment_type = models.CharField(max_length=60,choices = EMPLOYMENT_TYPE_CHOICES,
default = 'Permanent')
duration = models.CharField(max_length=60)
visa_type = models.CharField(max_length=60)
remarks = models.TextField(blank = True , null=True)
def __str__(self):
return self.project
class Meta:
verbose_name_plural = "Manpower_Requirement"
class Fulfillment(models.Model):
candidate_name = models.CharField(max_length=60)
manpower_requirement = models.ForeignKey(ManpowerRequirement, on_delete=models.CASCADE)
passport_number = models.CharField(blank = True, max_length=60)
subcontract_vendors = models.CharField(max_length=200,blank = True , null=True ,default='')
joined_date = models.DateField(blank = True, null = True, default = '')
remarks = models.TextField( blank = True,null = True)
def __str__(self):
return self.candidate_name
class Meta:
verbose_name_plural = "Fulfillment"
class FulfillmentStatus(models.Model):
fulfillment = models.ForeignKey(Fulfillment, on_delete=models.CASCADE)
status = models.CharField(max_length=60)
status_date = models.DateField()
remarks = models.TextField( blank = True, null = True )
def __str__(self):
return self.fulfillment.candidate_name
class Meta:
verbose_name_plural = "FulfillmentStatus"
I don't know how to do the same using bulk_create for Fulfillment and FulfillmentStatus models which are related to ManpowerRequirement. Csv file which I recieve in order to bulkcreate for Fulfillment consists of all the fields of ManpowerRequirement and all fields of Fulfillment and FulfillmentStatus excluding the foreign keys and id fields.
in the past I had the same problem; I solved in that way
Assuming that in a single CSV row you have data for all models I'd go for
create a mapping between the main model and the linked ones (you could use row index as key
use bulk_create() on the main model
iterate the dict and use bulk_create() for the linked modules
items = []
mrs = []
for row in list_of_dict:
mr = ManpowerRequirement(...)
mrs.append(mr)
f = ManpowerRequirement(...)
fs = FulfillmentStatus(...)
items.append((mr, f, fs))
# create all Manpower Requirements
ManpowerRequirements.objects.bulk_create(mrs)
a = []
for mr, f, fs in items:
f.manpower_requirement = mr
a.append(f)
# create all Fulfillments
Fulfillment.objects.bulk_create(a)
a = []
for mr, f, fs in items:
fs.fulfillment = f
a.append(fs)
# create all FulfillmentStatus
FulfillmentStatus.objects.bulk_create(a)
not sure if you can do some optimization in looping but it should solve the problem with just 3 queries
For related models we can do like this
class FulfillmentAPI(APIView):
def post(self, request):
paramFile = io.TextIOWrapper(request.FILES['fulfillmentfile'].file)
dict1 = csv.DictReader(paramFile)
list_of_dict = list(dict1)
objs = [
Fulfillment(
manpower_requirement=ManpowerRequirement.objects.get(project=row['project'], position=row['position'], quantity=row['quantity'], requested_date=row['requested_date'],),
remarks=row['remarks'],
candidate_name=row['candidate_name'],
passport_number=row['passport_number'],
joined_date=row['joined_date'],
subcontract_vendors=row['subcontract_vendors'],
)
for row in list_of_dict
]
try:
msg = Fulfillment.objects.bulk_create(objs)
returnmsg = {"status_code": 200}
print('imported successfully')
except Exception as e:
print('Error While Importing Data: ', e)
returnmsg = {"status_code": 500}
return JsonResponse(returnmsg)
In my Djago-React Application I am creating a form which has dynamic fields as well as an option to upload the document
For that I am using multipart/form-data and its working fine when I am using it only to upload the document but as soon as I enter the data in dynamic fields the serializer stops handling the data.
Example data:
Form Data:
transaction_no: 2341
document: (binary)
items[0][product]: 5
items[0][quantity]: 1
items[0][fault]: Repairable
items[1][product]: 4
items[1][quantity]: 2
items[1][fault]: Return
allotment: 122
Response:
{"items":[{"product":["This field is required."]},{"product":["This field is required."]}]}
Serializer:
class DeliveredItemsSerializer(serializers.ModelSerializer):
class Meta:
model = DeliveredItems
fields = "__all__"
class DeliveredSerializer(serializers.ModelSerializer):
items = DeliveredItemsSerializer(many=True,required=False)
class Meta:
model = Delivered
fields = "__all__"
def create(self, validated_data):
items_objects = validated_data.pop('items', None)
prdcts = []
try:
for item in items_objects:
i = DeliveredItems.objects.create(**item)
prdcts.append(i)
instance = Delivered.objects.create(**validated_data)
print("prdcts", prdcts)
instance.items.set(prdcts)
return instance
except:
instance = Delivered.objects.create(**validated_data)
return instance
def update(self, instance, validated_data):
items_objects = validated_data.pop('items',None)
prdcts = []
try:
for item in items_objects:
print("item", item)
fk_instance, created = DeliveredItems.objects.update_or_create(pk=item.get('id'), defaults=item)
prdcts.append(fk_instance.pk)
instance.items.set(prdcts)
instance = super(DeliveredSerializer, self).update(instance, validated_data)
return instance
except:
instance = super(DeliveredSerializer, self).update(instance, validated_data)
return instance
Models:
class DeliveredItems(models.Model):
choices = (('Repairable', 'Repairable'),('Return', 'Return'), ('Damage', 'Damage'), ('Swap Return', 'Swap Return'))
product = models.ForeignKey(Product, on_delete=models.CASCADE)
quantity = models.IntegerField(default=0)
fault = models.CharField(max_length=100, default="Repairable", choices=choices)
class Delivered(models.Model):
allotment = models.ForeignKey(Allotment, on_delete=models.CASCADE)
delivered = models.BooleanField(default=False)
items = models.ManyToManyField(DeliveredItems)
document = models.FileField(upload_to='delivered/', null=True, blank=True)
owner = models.ForeignKey(User, on_delete=models.CASCADE)
How can I handle the dynamic data that don't come in a list and comes as something like this items[0][product]: 5?
I need the data to be in this format:
{
"transaction_no": 2335,
"delivered": false,
"document": {
"uid": "rc-upload-1599739825759-7"
},
"items": [
{
"product": 4,
"quantity": "12",
"fault": "Repairable"
},
{
"product": 5,
"quantity": "1",
"fault": "Return"
}
],
"allotment": 116
}
please try this one if you want to handle form data in DRF
in view, please inherit viewsets.ModelViewSet
def dict_shallow_copy(d):
return dict(d.items())
def get_serializer(self, *args, **kwargs):
if "data" in kwargs:
data = kwargs.get("data", {})
if isinstance(data, dict):
data = dict_shallow_copy(data)
//get your data here, sameple like this
data['items'] = self.kwargs
data['transaction_no'] = self.kwargs
kwargs['data'] = data
else:
raise ValidationError("Invalid data type")
return super().get_serializer(*args, **kwargs)
This is my Model class
class SubJobs(models.Model):
id = models.AutoField(primary_key = True)
subjob_name = models.CharField(max_length=32,help_text="Enter subjob name")
subjobtype = models.ForeignKey(SubjobType)
jobstatus = models.ForeignKey(JobStatus, default= None, null=True)
rerun = models.ForeignKey(ReRun,help_text="Rerun")
transfer_method = models.ForeignKey(TransferMethod,help_text="Select transfer method")
priority = models.ForeignKey(Priority,help_text="Select priority")
suitefiles = models.ForeignKey(SuiteFiles,help_text="Suite file",default=None,null=True)
topofiles = models.ForeignKey(TopoFiles,help_text="Topo file",default=None,null=True)
load_image = models.NullBooleanField(default = True,help_text="Load image")
description = models.TextField(help_text="Enter description",null=True) command = models.TextField(help_text="Command",null=True)
run_id = models.IntegerField(help_text="run_id",null=True)
pid_of_run = models.IntegerField(help_text="pid_of_run",null=True)
hold_time = models.IntegerField()
created = models.DateTimeField(default=timezone.now,null=True)
updated = models.DateTimeField(default=timezone.now,null=True)
finished = models.DateTimeField(null=True)
The user might want to update a entry and may choose to update only few fields among these. How do I write a generic update statement that would update only the fields that were passed?
I tried this.
def update_subjob(request):
if (request.method == 'POST'):
subjobs_subjobid = request.POST[('subjob_id')]
post_data = request.POST
if 'subjob_name' in post_data:
subjobs_subjobname = request.POST[('subjob_name')]
if 'subjob_type' in post_data:
subjobs_subjobtype = request.POST[('subjob_type')]
if 'rerun_id' in post_data:
subjobs_rerun_id = request.POST[('rerun_id')]
if 'priority_id' in post_data:
subjobs_priority_id = request.POST[('priority_id')]
if 'transfer_method' in post_data:
subjobs_transfermethod = request.POST[('transfer_method')]
if 'suitefile' in post_data:
subjob_suitefile = request.POST[('suitefile')]
if 'topofile' in post_data:
subjob_topofile = request.POST[('topofile')]
try:
subjobinstance = SubJobs.objects.filter(id=subjobs_subjobid).update(subjob_name=subjobs_subjobname,
updated=datetime.now())
except Exception as e:
print("PROBLEM UPDAING SubJob!!!!")
print(e.message)
How do I write a generic update to update only those fields which are sent in request.POST?
You'd better use Forms. But if you insist on your code it could be done like this.
Suppose you have variable field_to_update where every field that you are waiting in request is listed.
subjobs_subjobid = request.POST[('subjob_id')]
field_to_update = ('subjob_name','subjob_type', 'rerun_id', 'priority_id', 'transfer_method', 'suitefile', 'topofile')
post_data = request.POST
to_be_updated = {field: post_data.get(field) for field in field_to_update if field in post_data}
# then you need to get the object, not filter it
try:
subjobinstance = SubJobs.objects.get(id=subjobs_subjobid)
subjobinstance.update(**to_be_updated, updated=datetime.now())
except ObjectDoesNotExist: # from django.core.exceptions
print('There is no such object') # better to use logger
except Exception as e:
print("PROBLEM UPDAING SubJob!!!!")
print(e.message)
i wrote a test case:
class MyTestCreateFilter(TestCase):
def test_createfilter(self):
test_filter = Filter(user_profile_id= 3,
keyword = 'ca',
industry = 'it',
zip_code = '50002',
distance = 30,
creation_date = datetime.date.today(),
last_run_date = datetime.date.today()
)
test_filter_form = FilterForm(instance=test_filter)
self.assertEqual(test_filter_form.is_valid(), False)#without data
test_filter_form = FilterForm({'user_profile_id':3,'keyword': 'ca','industry':'it','zip_code':'50002','distance':30,'creation_date': datetime.date.today(),
'last_run_date': datetime.date.today() }, instance=test_filter)
print test_filter_form.is_valid()
giving the error:
DoesNotExist: UserProfile matching query does not exist.
this is my form.how to write test case:
class FilterForm(forms.ModelForm):
class Meta:
model=Filter
exclude=('user_profile','creation_date','last_run_date')
widgets = {
'zip_code': forms.TextInput(attrs={'placeholder': "e.g. 20708"}),
}
def clean(self):
user_profile = self.instance.user_profile
keyword = self.cleaned_data.get("keyword")
if Filter.objects.filter(user_profile=user_profile, keyword=keyword).exclude(id=self.instance.id).count() > 0:
msg = u"A filter with that keyword already exists!"
self._errors["keyword"] = self.error_class([msg])
return self.cleaned_data
when i test the form giving this error:
user_profile = self.instance.user_profile
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 343, in get
raise self.field.rel.to.DoesNotExist
DoesNotExist
how to solve it?
Simply creating model object will not create the record in the database.
Use .objects.create to create a record.
test_filter = Filter.objects.create(
user_profile_id= 3,
keyword = 'ca',
industry = 'it',
zip_code = '50002',
distance = 30,
creation_date = datetime.date.today(),
last_run_date = datetime.date.today()
)
or use save:
test_filter = Filter(...)
test_filter.save()