Django update_or_create with potentially null idenitfier - python

I have a function in Django that accepts a post request. If that post request includes an id, I want to update that object. If the post request sends a blank/null id, I'd like to create a new object and have Django set the ID (primary key) of the model.
Here is what I have:
def save_user_roster_to_db():
name = request.data['name']
id = request.data['id'] # this could be null/blank or be an ID
try:
user_roster = SavedUserRoster.objects.update_or_create(
id=id,
defaults={
"name": name,
}
)
return user_roster.pk
except:
raise Exception
However, this seems to try to assign the blank/null value as the ID when creating a new object. How can I tell Django to set its own ID (which is the primary key) if it's creating a new object?

You can't use update_or_create with the id field. Because in the current situation, id can have a value of None, and the Django model can't create the object with an id of None.
So I think you can try like the following.
def save_user_roster_to_db():
name = request.data['name']
id = request.data['id'] # this could be null/blank or be an ID
try:
if id:
user_roster = SavedUserRoster.objects.get(pk = id).update(name = name)
else:
user_roster = SavedUserRoster.objects.create(name = name)
return user_roster.pk
except:
raise Exception

Traditionally id is auto generated and always unique. In this case replacing None id will create exception when you create record for first time.
There are two possible options.
OPTION 1:
Create another unique_identified i.e username or email.
def save_user_roster_to_db():
name = request.data['name']
unique_identified = request.data['unique_identified'] # this could be null/blank or be an ID
user_roster, is_created = SavedUserRoster.objects.update_or_create(
unique_identified=unique_identified,
defaults={
"name": name,
}
)
return user_roster.pk
OPTION 2:
For Create:
Get the last id from database and add 1 with last id, so that It will be the next id value, It will avoid the None exception.
For Update:
It will update the existence record against id
def save_user_roster_to_db():
name = request.data['name']
id = request.data['id'] # this could be null/blank or be an ID
if id is None:
id = int(MyUserModel.objects.all().last().id)+1
user_roster, is_created = SavedUserRoster.objects.update_or_create(
id=id,
defaults={
"name": name,
}
)
return user_roster.pk

Related

Python Flask SqlAlchemy Add Database Model Name dynamically in For Loop

I am not that familiar with Python and SQLAlchemy so please be patient :)
I need to capture if, within a FORM that holds multiple ICONS(files), one or more ICONS have been changed when editing the record.
To see which ICONS have been changed I created an Object holding the changes with "Database Model Name" as the "Key" and its "Value"
{'icon': <FileStorage: 'fire.png' ('image/png')>}
key = used as database model name
value = file.filename
now when I try the get the data within a for loop and add this data to the Database model, nothing happens and it looks like I am not really accessing variable "k" in the loop.
for k, v in notequalat.items():
responseteamdata.k = v.filename
My question is, how can I combine the Database model class "responseteamdata" and the variable "k" so that I can add the changes to the database model dynamically.
here is the full code:
if not notequalat:
try:
responseteamdata.title = title
responseteamdata.abbreviation = abbreviation
responseteamdata.isfireteam = booleanisfireteam
responseteamdata.iconposition = newlatlng
db.session.commit()
except IntegrityError:
db.session.rollback()
db.session.close()
res = make_response(jsonify("message ", "Error Updating the Team"), 500)
return res
else:
responseteamdata.title = title
responseteamdata.abbreviation = abbreviation
responseteamdata.isfireteam = booleanisfireteam
responseteamdata.iconposition = newlatlng
for k, v in notequalat.items():
responseteamdata.k = v.filename
db.session.commit()
dbevent = "updated"
db.session.close()
To be able to dynamically assign the Table Column Name the following command has been working for me:
setattr(DB-Object, ColumnName, Value)
which means in my case:
setattr(responseteamdata, k, v.filename)

Odoo - Iterate through field, take the values and put them in a new field

Hay I am new to Odoo Customizing and Python and wanted to know how I can iterate through a field and take the values out of the field and put them in a new one.
The field I want to iterate through contains multiple email adresses. I want to iterate through these email adress fields, collect the email adresses and store them together in a new field.
For that I need a function.
The field I want to iterate through:
My One2many field contains multiple mail adresses which I want to iterate through and collect.
field_contacts_customer_info = fields.One2many(
'contacts.customer.information', 'another_id', string='Contacts for customer information')
The field I want to store the collected email adresses in:
selected_email = fields.Char(compute='compute_email')
This is my class:
I want to collect all the email adresses from the mail_contacts field.
_name = 'contacts.customer.information'
_rec_name = 'name_contacts'
name_contacts = fields.Many2one(
'res.partner', string="Person", domain = [('is_company', '=', False)])
mail_contacts = fields.Char(
related = 'name_contacts.email' ,string="Email")
another_id = fields.Many2one('res.partner', string="AnotherID")
My Try: This function collects only the last set record of the field_contacts_customer_info field and puts this record in the selected_email field of every company.So it does not work right. It should collect all the mails of the field_contacts_customer_info field for every company seperated and then put them in the selected_mail field of the belonging company.
#api.onchange('field_contacts_customer_info.mail_contacts')
def compute_email(self):
list_email = []
for record in self:
if record.is_company:
for element in record.field_contacts_customer_info:
if element.name_contacts:
list_email.append(element.mail_contacts)
for email in list_email:
self.selected_email = email
Thanks.
You need to iterate over self which is a record set and loop over field_contacts_customer_info field to get mail_contacts field values.
#api.depends('field_contacts_customer_info.mail_contacts')
def get_email(self):
for record in self:
record.selected_email = ','.join(info.mail_contacts for info in record.field_contacts_customer_info if info.mail_contacts)
Then set the compute attribute to get_email:
selected_email = fields.Char(string="Mail4Info", compute='get_email')
You can check the ORM documentation on how to use the computed fields.
Edit (compute method):
You are setting the value of selected_email to each element of list_email, after the compute_email is executed the value of selected_email will always be the last value of list_email.
The last for loop is executed each time we loop over record.field_contacts_customer_info, it should be at the same level as the second loop.
The list_email is declared before we loop over records (it is not reset in the loop), after the first record, each record will use the email values of previous records.
When record.is_company is evaluated to False, the compute method will not assign a field value, you should see the following error:
ValueError: Compute method failed to assign {record description}.selected_email
It happens because the compute method must assign a field value
Example:
#api.depends('field_contacts_customer_info.mail_contacts')
def compute_email(self):
for record in self:
list_email = []
if record.is_company:
for element in record.field_contacts_customer_info:
if element.name_contacts:
list_email.append(element.mail_contacts)
emails = ""
for email in list_email:
emails += email + " "
record.selected_email = emails
else:
record.selected_email = ""
You can change the list_email type to a string and avoid looping again to get the field value:
Example:
#api.depends('field_contacts_customer_info.mail_contacts')
def compute_email(self):
for record in self:
list_email = ""
if record.is_company:
for element in record.field_contacts_customer_info:
if element.name_contacts:
list_email += element.mail_contacts
record.selected_email = list_email

How to Handle When Request Returns None

I have a list of IDs which corresponds to a set of records (opportunities) in a database. I then pass this list as a parameter in a RESTful API request where I am filtering the results (tickets) by ID. For each match, the query returns JSON data pertaining to the individual record. However, I want to handle when the query does not find a match. I would like to assign some value for this case such as the string "None", because not every opportunity has a ticket. How can I make sure there exists some value in presales_tickets for every ID in opportunity_list? Could I provide a default value in the request for this case?
views.py
opportunities = cwObj.get_opportunities()
temp = []
opportunity_list = []
cw_presales_engineers = []
for opportunity in opportunities:
temp.append(str(opportunity['id']))
opportunity_list = ','.join(temp)
presales_tickets = cwObj.get_tickets_by_opportunity(opportunity_list)
for opportunity in opportunities:
try:
if opportunity['id'] == presales_tickets[0]['opportunity']['id']:
try:
for presales_ticket in presales_tickets:
cw_engineer = presales_ticket['owner']['name']
cw_presales_engineers.append(cw_engineer)
except:
pass
else:
cw_engineer = 'None'
cw_presales_engineers.append(cw_engineer)
except AttributeError:
cw_engineer = ''
cw_presales_engineers.append(cw_engineer)
So, lets say you have a Ticket model and Opportunity model. Connected via a foreign key.
class Opportunity(models.Model):
... some fields here ...
class Ticket(models.Model):
opportunity = models.ForeignKey(Opportunity)
and in your view, you get a list of opportunity ids
def some_view(request):
ids = request.GET['ids']
It sounds, like what you want is to fetch all the tickets for the supplied opportunities and add some default processing for the opportunities that do not have tickets. If that is the case, why not do something like
def some_view(request):
ids = request.GET['ids']
tickets = Ticket.objects.filter(opportunity__id__in=ids)
results = []
for ticket in tickets:
result = ... do your thing here ...
results.append(result)
# now handle missing opportunities
good_ids = tickets.values_list('opportunity__id', flat=True).distinct()
for id in ids:
if id not in good_ids:
result = ... do your default processing ...
results.append(result)
Is that what you are trying to do?

Odoo: How to create many records in Transient.Model?

This code only creates one record. What is wrong?
class PartnerTagCreate(models.TransientModel):
""" Choose tags to be added to partner."""
_name = 'partner.tags.create'
_description = __doc__
market_id = fields.Many2one('partner.tags', string='Market Tag')
application_id = fields.Many2one('partner.tags', string='Application Tag')
partner_id = fields.Integer()
#api.multi
def create_contact_tag(self):
for record in self.env['sale.order.line'].browse(self._context.get('active_ids', [])):
vals = {}
vals['partner_id'] = record.order_partner_id
self.write(vals)
return True
I need this function to create one record for each order_partner_id I selected before opening the wizard...
How to achieve that?
Here my new code (function) ...
def create_contact_tag(self):
sale_order_line_ids = self.env['sale.order.line'].browse(self._context.get('active_ids', []))
for partner in sale_order_line_ids:
values = {}
values['partner_id'] = partner.order_partner_id
self.create(values)
return {}
This creates one record for marketing_id and/or application_id and dedicated records for each partner_id in the record.
You use the 'create' method to create new records; this is the same for TransientModel as for the persistent Model.
So, replace
self.write(vals)
by
self.create(vals)
and you should be fine.

Django queryset not returning all rows

Hi I have below setup.
Django 1.9,
mongoDB,
pymongo 2.7,
mongoengine 0.9
I have written an API to store logs at backend server userwise. Below is sample of table
user subject registered changed_on
abc eng Y "2018-04-18T00:00:00Z"
abc maths N "2018-04-18T00:10:00Z"
xyz eng Y "2018-04-18T00:10:00Z"
I also have read API for this in which we give user name and timestamp for filter like below:
{
"user" : "abc",
"from_date" : "2018-04-18T00:00:00Z"
}
The line in serializers.py which is applying filter is
Logs.objects.filter(user__iexact='abc',changed_on__gte=from_date)
Now Sometimes when I add new log and retrieve it from postman, it is not working. I have to restart django server and then it gives me newly added row.
I dont understand why this is happening.
EDIT1 : Full Serializer.py
from rest_framework import serializers
class GetUserLogs(serializers.Serializer):
user = serializers.CharField(label=_("USER"))
token = serializers.CharField(label=_("Token"))
from_date = serializers.CharField(label=_('From date'), default="")
till_date = serializers.CharField(label=_('Till date'), default=datetime.datetime.now().isoformat().split(".")[0]+'Z')
def validate(self, attrs):
user = attrs.get('user')
token = attrs.get('token')
from_date = attrs.get('from_date')
if user:
tokendetails = validate_token(token)
if not tokendetails:
msg = _('Invalid token.')
raise serializers.ValidationError(msg)
else:
userdetails = tokendetails.user
if userdetails.check_user(user):
rows = Logs.objects.all().filter(user__iexact=user,changed_on__gte=from_date, changed_on__lte = till_date)
print(len(rows))
else:
msg = _('Invalid USER)
raise serializers.ValidationError(msg)
else:
msg = _('Must include "USER".')
raise serializers.ValidationError(msg)
attrs['rows'] = rows
return attrs
So After a lot of debugging for almost a day, I found that the default value of till_date that we are setting to current timestamp of system is not increasing as time increase. So I changed default value to empty string and added below code after reading value of till_date.
if till_date == "":
till_date = datetime.datetime.now().isoformat().split(".")[0]+'Z'
It will reset the till_date value to current timestamp every time its value is missing.
Hope it will help someone someday.

Categories