Zenpy; custom fields are not being updated - python

So within Zendesk I created these custom fields as follows:
and now I am trying to create a user with all these fields filled with preset values. But when I try to create users, like follows:
for row in df.iloc[1:11].fillna(0).iterrows():
user = User(name = row[1].first_name,
email = row[1].email,
lifetime_value = row[1].purchased_total_value,
first_order = row[1].first_purchased,
last_order = row[1].last_purchased,
products_ordered = row[1].purchased_product_count,
total_orders = row[1].purchased_unique_orders,
total_returns = row[1].total_returns,
products_returned = row[1].products_returned,
pro_account = pd.notna(row[1].proaccount_deal),
verified=True)
created_user = zenpy_client.users.create(user)
All users are created however only with email and name fields filled up and all the custom fields which I created are empty. Zenpy's User() function is not very informative in this regard however it accepts **kwargs** and as per json representations in documentation, it should work in theory. Any workaround or my mistakes in this regard?

Have you tried using user_fields?
for row in df.iloc[1:11].fillna(0).iterrows():
uf = {"lifetime_value" : row[1].purchased_total_value,
"first_order" : row[1].first_purchased,
"last_order" : row[1].last_purchased,
"products_ordered" : row[1].purchased_product_count,
"total_orders" : row[1].purchased_unique_orders,
"total_returns" : row[1].total_returns,
"products_returned" : row[1].products_returned,
"pro_account" : pd.notna(row[1].proaccount_deal),
"verified":True}
user = User(name = row[1].first_name, email = row[1].email, user_fields=uf)
created_user = zenpy_client.users.create(user)
This should work, but if it doesn't you could try creating the user first and then querying for them, and then seeing all of your custom fields in user.user_field.
Doing so would allow you to see all the available custom user fields you can enter in. From there you can update the object and zenpy_client.users.update(user) it into the system.

Related

Django add dynamic fields with dynamic name attributes to front end and then insert it to Model

I have a Model which consists of almost 500+ fields. I want to take data through the front-end Form. But the issue is I can’t display all the 500 fields.
what I want is I will add the fields which I need. like on day-1 I will add 3 fields. food = 230 , petrol = 500, rent = 700. on the second day, fields may vary. I can add 20 fields on the second day. on the third day data may be of different fields. like petrol = 200, cement= 5000 , steel = 100 etc etc and so on!
Simple Solution is: I displayed all fields in HTML and submitted it to view and received in View function through Post Request! and then save() . But i want it to be dynamic!
if request.method == 'POST':
print("this is post")
# print()
food_expense=request.POST['food']
petrol_expense=request.POST['petrol']
easyload_expense=request.POST['easyload']
labour_expense = request.POST['labour']
equipments_expense = request.POST['equipments']
rent_expense = request.POST['rent']
cement_expense = request.POST['cement']
steel_expense = request.POST['steel']
other1_expense = request.POST['other1']
other2_expense = request.POST['other2']
other3_expense = request.POST['other3']
..... .... fields upto 500+
# expense_paidby = request.POST['paid_by']
expense_paidby_id = request.POST['paid_by']
paidby= PaidBy.objects.get(id=expense_paidby_id)
# expense_paidby = PaidBy.objects.get(name__icontains = request.POST['paid_by'])
projectname_id= request.POST['pname']
project_in_which_expenses_made=ProjectName.objects.get(id=projectname_id)
# project_in_which_expenses_made = request.POST['project_name']
date = request.POST['date']
# total = food_expense+petrol_expense+easyload_expense+labour_expense+equipments_expense+rent_expense
# print(petrol_expense, projectname_id , project_in_which_expenses_made)
inst= ProjectExpenses(food=food_expense,petrol=petrol_expense,easyload=easyload_expense ,labour=labour_expense ,
equipments=equipments_expense ,rent=rent_expense,cement=cement_expense,steel=steel_expense,
other1=other1_expense, other2=other2_expense, other3=other3_expense, date_of_expense=date, PaidByName=paidby,
expenses_in_project = project_in_which_expenses_made)
#in query i will have 500 fields.
inst.save()
what I want is how I can create dynamic input fields with name attributes as I have in the database.
how should I create these fields dynamically in the front end and then how I will handle it in view function instead of writing all 500+ fields through “value = request. post[“500+ fields line by line”]” means how I can handle it dynamically only those fields which come to view function. and then save it to DB.
data will only insert in selected fields all remaining will be zero by default.
I will appreciate forum Help.
I am not sure if I understood your question right. Perhaps a more minimal working example would clarify.
But, have you tried using model forms, such as CreateView?
You can dynamically create input forms and handling based on your model definition by adding a CreateView to your urlpatterns.
from django.views.generic.edit import CreateView
from .models import ProjectExpenses
urlpatterns += [path("new-project-expense", CreateView.as_view(model=ProjectExpenses, fields="__all__"))]
You can configure the "fields" argument to be a selection of your 500 model fields.

Django Can't save record with ForeinKey

I am familiar with the Django and I am using Django framework(2.2.2) in one of my website, But I am getting one weird issue for saving record with the foreign key:
I have following two models
class quick_note(models.Model):
note_id = models.AutoField(primary_key=True)
meeting_date = models.DateField(max_length=55)
title = models.TextField(blank = True,null=True)
headline = models.TextField(blank = True,null=True)
class quick_note_details(models.Model):
meeting_id = models.ForeignKey(quick_note,on_delete = models.CASCADE)
summary = models.TextField(default='',null=True)
Following is the code I have used for saving:
quick_note_details_data = quick_note_details(summary = summary_data,meeting_id = 1)
quick_note_details_data.save()
Using this I am getting following error:
ValueError: Cannot assign "2": "quick_note_details.meeting_id" must be a "quick_note" instance.
Then, I have tried the following approach suggested in the following question,
Django: ValueError when saving an instance to a ForeignKey Field
quick_note_obj = quick_note.objects.get(note_id = note_id)
quick_note_details_data = quick_note_details(summary = summary_data,meeting_id = quick_note_obj)
quick_note_details_data.save()
Using this I am getting following error:
django.db.utils.ProgrammingError: column "meeting_id_id" of relation "website_quick_note_details" does not exist
LINE 1: INSERT INTO "website_quick_note_details" ("meeting_id_id", "...
I don't have a column like meeting_id_id in the model anywhere, then why I am getting this error?
I have been searching for this long time, but didn't get any solution,
Hope I will get help here.
change meeting_id to meeting_id_id , try following
quick_note_details_data = quick_note_details(summary = summary_data,meeting_id_id = 1)
So, I had got my answer days back, so decided to answer so this will help people like me,
Basically, Django appends a suffix to the foreign_key columns,
e.g if your columns name is note ,Django will assume this as note_id,
So,To avoid this I had done only the following:
note_id = models.ForeignKey(quick_note,on_delete = models.CASCADE,db_column='note_id')
By using db_column property, now the foreign key will refer as note_id,
This solves my issue.

Python - How to Set Custom Fields using NetSuite webservices

I'm trying to be able to set a custom fields in NetSuite using webservices.
The WSDL I'm using is: https://webservices.netsuite.com/wsdl/v2017_2_0/netsuite.wsdl
Currently I'm testing it on creating a customer. Here's what I have so far:
def add_customer():
client = login_client()
RecordRef = client.get_type('ns0:RecordRef')
Customer = client.get_type('ns13:Customer')
customer = Customer(
companyName='TEST',
subsidiary = RecordRef(internalId='5', type='subsidiary')
)
response = client.service.add(customer)
print(response)
add_customer()
This Works perfectly, but now I'm trying to set a custom field with id custfield1
After doing some searching, I found:
http://www.netsuite.com/help/helpcenter/en_US/srbrowser/Browser2016_2/schema/other/customfieldlist.html?mode=package
From this link I know that I'll be needing to use CustomFieldRef, I'm just not sure how it would be implemented.
I found a way to do this:
def add_customer():
client = login_client()
RecordRef = client.get_type('ns0:RecordRef')
StringCustomFieldRef = client.get_type('ns0:StringCustomFieldRef') #StringCustomFieldRef
CustomFieldList = client.get_type('ns0:CustomFieldList') #To go from object to list
#Cust field 1
acctName = StringCustomFieldRef()
acctName.internalID = '1569'
acctName.scriptId = 'custentity_account_name'
acctName.value = 'testData'
#custField2
acctID= StringCustomFieldRef()
acctID.internalId= '1596'
acctID.scriptId= 'custentity_sf_account_id'
acctID.value = 'FIELD DATA'
Customer = client.get_type('ns13:Customer')
customer = Customer(
companyName='TEST',
entityId='TEST ID',
subsidiary = RecordRef(internalId='5', type='subsidiary'),
customFieldList = CustomFieldList([acctID,acctName]) #List of cust objects
)
response = client.service.add(customer)
print(response)
add_customer()
You have to use the Ref type for the field you are working with: https://system.na1.netsuite.com/app/help/helpcenter.nl?fid=section_n3458179.html

automatically set field values for models in Django

Is there a way to automatically set field values for models in Django when defining the model?
I need t define some values of fields automatically in my model using function.
my function get input image path calculate and I need that calculation results to define my database fields in Django.
first to I want is something like this :
my view :
def myview(request):
uploadimages = UploadImagesForm(request.POST or None, request.FILES or None)
if uploadimages.is_valid():
# Get the images that have been browsed
if request.FILES.get('multipleimages', None) is not None:
images = request.FILES.getlist('multipleimages')
for image in images:
MyModel.objects.create(field1=request.user,field2=image)
that doesn't work because to work my function need first to upload image in server to get the path to work.
any idea how to define my model automate using my function ?
update
instance = MyModel.objects.create(user=request.user, upload=image)
instance.field1 = name
instance.field2 = myvalue
instance.field3 = myvalue2
instance.field4 = myvalue3
instance.field5 = myvalue4
instance.save()
error in this code is the my function cant understand the image path to create the calculation to set the values in fields.
if I use this :
MyModel.objects.create(user=request.user, upload=image)
instance = MyModel.objects.create(user=request.user, upload=image)
instance.field1 = name
instance.field2 = myvalue
instance.field3 = myvalue2
instance.field4 = myvalue3
instance.field5 = myvalue4
instance.save()
that work but create me duplicates in database .
You can try:
instance = MyModel.objects.create(field1=request.user, field2=image)
instance.field3 = myfunc(image)
instance.field4 = myfunc(image)
instance.save()

How do I order by date when using ReferenceProperty?

I have a simple one-to-many structure like this:
class User(db.Model):
userEmail = db.StringProperty()
class Comment(db.Model):
user = db.ReferenceProperty(User, collection_name="comments")
comment = db.StringProperty()
date = db.DateTimeProperty()
I fetch a user from by his email:
q = User.all() # prepare User table for querying
q.filter("userEmail =", "az#example.com") # apply filter, email lookup
results = q.fetch(1) # execute the query, apply limit 1
the_user = results[0] # the results is a list of objects, grab the first one
this_users_comments = the_user.comments # get the user's comments
How can I order the user's comments by date, and limit it to 10 comments?
You will want to use the key keyword argument of the built-in sorted function, and use the "date" property as the key:
import operator
sorted_comments = sorted(this_users_comments, key=operator.attrgetter("date"))
# The comments will probably be sorted with earlier comments at the front of the list
# If you want ten most recent, also add the following line:
# sorted_comments.reverse()
ten_comments = sorted_comments[:10]
That query fetches the user. You need to do another query for the comments:
this_users_comments.order('date').limit(10)
for comment in this_users_comments:
...

Categories